Header Shadow Image


Perl Reference / Cheat Sheet

PERL REFERENCE / CHEAT SHEET

Handy, searchable Perl reference guide for both beginners and intermediate Perl developers.  This is something of a reference and a tutorial combined into one.  If you like, don’t forget to post a comment.

CONTENTS

Assignment Operators
Operators, Precedence, Example and Associativity
Numeric Literals
Bitwise Operator Result Table
String Literals
Variables
String Operations
Built In Literals
Control Blocks
Loop Flow Control Modifiers
Printing Format Specifiers
Print Specifier Modifiers
Alternative Quoting
Pack Unpack Modifiers
PERL REGULAR EXPRESSIONS
Optional Delimeters
Operators Matching Modifiers
Operator Table
Y Modifier Table
Perl REGEX Metacharacters
Perl File IO
Perl File Lock Types
Script Arguments
File Test Operators

Assignment Operators

NOTE: Where you see <operator>= means end operation is assigned to the respective variable.  For example ‘$v |= 3‘  means 3 is ORed with $v and result is reassigned into $v overwriting previous $v value.

Operator Code Description
= $v=34; Assign 34 to $v
+= $v += 7; Add 7 to $v.  $v will then equal 41
-= $v -= 7; Subtract 7 from $v. $v will now equal 34
.= $string.=”25″; Concatenate string “25″ to current value of $string.
*= $v*=2; Multiply $v by 2 and reassign back to $v$v will then equal 68
/= $v/=2; Devide $v by 2 and reassign back to $v.  $v will then equal 34
**= $v **=3; Exponent. Equivalent to 34 * 34 * 34
%= $v %= 3; Modulus.  Assign remainder of dividing 34 by 3 to $v.  In this case remainder is 1 which is what $v will equal
x= $string x= 5; Repeat $string 5 times and assign to $string
<<= $v <<= 2; Left-shift bits in $v
>>= $v >>= 3; Right-shift bits in $v
&= $v &= 5; Five is bitwise-ANDed with $v
|= $v |= 3; Three is bitwise-ORed with $v
^= $v ^= 2; Bitwise-exclusive ORed with $v
$v–; Decrement $v by 1 and reassign to $v. Same as $v -= 1;  (Two plus and two minus signs)
++ $v++; Increment $v by 1 and reassign to $v Same as $v += 1;

Operators, Precedence, Example and Associativity

Operator Detail Example Association
() [] {} Function call, Array, Hash
  • FUNCTION:
    sub fName {
    my $arg1 = $_[0];
    my $arg2 = $_[1];
    my $alist = @_;
    my ($s, @a) = @_;
    $arg3 = …
    function_code…
    };
    &fName (87.65, 1082, @myA, $myS, “My String”, ‘c’, <OTHER PARAMETERS>);

    (NOTE: May omit ‘&’ from function call)
  • ARRAY: my @a = ( ‘a’, ‘b’, ‘c’);
    print $a[1];

     

    displays a on the screen.

  • HASH: my %h = ( ‘a’, ‘1′, ‘b’, ‘2′, ‘c’, ‘3′);
    OR
    my %h = ( a => ‘1′, b => ‘2′, c => ‘3′ );
    print $h{‘a’};

    displays 1 on the screen.

L > R
-> De-reference operator my $mp = \@a; $mp->[0]; L > R
++ – Auto increment / decrement (Two plus and two minus signs) Post-increment: $v++;    $v;

 

Pre-increment: $v; ++$v;

N/A
** Exponentiation my $a = $b ** $e; R > L
! ~ \ + – Logical not, bitwise not, unary plus, unary minus, backslash
  • ! $v
    EX: if ( ! $x ) { print “Not $x”; }
  • my $r = ~1001;
  • my $mp = \@a;  // Create memory reference.
  • + has no semantic effect.
  • my $r = $v;
R > L
=~  !~ Match and NOT match
  • printf “%s”, $1 if $v =~ /Hello/;
  • printf “%s”, $1 if $v !~ /Hello/;
L > R
* / % x Multiply, divide, modulus, string rep.
  • my $r = $v1 * $v2;
  • my $r = $v1/ $v2;
  • my $r = $v1 % $v2;
  • ‘Beer’ x 3;  ‘BeerBeerBeer’
L > R
+ – . Add, subtract, string concatenation
  • my $r = $v1 + $v2;
  • my $r = $v1 $v2;
  • my $strr = $str1 . $str2;
L > R
<< >> Bitwise left / right shift
  • my $r = $v1 << 3; # Shift 3 bits to LEFT
  • my $r = $v1 >> 3; # Shift 3 bits to RIGHT
L > R
-r -w -x -o etc Named unary operators.  (File test operators)
  • if ( -r /path/file.xyz ) {
    print “File exists\n”;
    }
  • if ( -d /path/file.syz ) {
    print “Directory exists\n”;
    }
N/A
< <= >= lt le gt ge Less, less or equal, greater or equal, less then,less then or equal, greater then, greater then or equal
  • if ( $v1 <= $v2 ) {
    print “v1 is less then or equal to v2\n”;
    }
  • if ($v1 ge $v2 ) {
    print “v1 is greater then or equal to v2\n”;
    }
N/A
== != <=> eq ne cmp Number / string tests: equal, not equal, equal, not equal, string compare
  • if ( $v1 == $v2 ) {
    print “v1 equals v2\n”;
    }
  • if ( $str1 <=> $str2 ) {
    print “str1 and str2 contain exact same value.\n”;
    }
  • <=> returns -1, 0, 1
N/A
& Bitwise AND
  • my $r = $v1 & $v2;
  • my $r = 514 & 370;
L > R
|  ^ Bitwise or and exclusive or (XOR)
  • my $r = $v1 | $v2;
  • my $r = $v1 ^ $v2;
L > R
&& Logical AND
  • if ( $v1 && $v2 ) {
    ….
    }
L > R
|| Logical OR
  • if ( $v1 || $v2 ) {

    }
L > R
.. Range operator: EX 1..100
  • foreach (1..100) {
    print;
    }
N/A
?: Conditional (Ternary): EX (1 <= 2 ) ? print “True” : print “False”
  • ( $str1 <=> $str2 ) ? $v1 += 1 : $v1 += 2;
R > L
=  +=  -=  *=  /=  %= Assignment
  • my $r %= $v1;
  • my $r -= $v1;
R > L
||= Conditional assignment.
  • “$va ||= 5″ reads if $va is FALSE (Is either: 0, undef, “”, ” ) assign 5 into $va.
R > L
, => Evaluate left side, discard result, then evaluate right side.
  • Used in hashes predominantly.
  • %h = ( “a” => “1″,
    “b” => “2″,
    “c” => “3″,
    “d” => “4″
    );
L > R
not Lower precedence then and long for !
  • if ( not $var1 ) {
    print “var1not TRUE. IE var1 is FALSE.\n”;
    }
R
and Long for &&
  • if ( $v1 < $v2 and $v3 < $v4 ) {
    print “Ok, executing self destruct seq.\n”;
    }
L > R
or  xor Long for || and ^
  • if ( $v1 < $v2 xor $v3 < $v4 ) {
    print “Ok, formatting your hard drive…\n”;
    }
L > R
; {) Scope modifiers
  • if ( ….. ) {
    print “Dont’ just sit there…do something!\n”;
    }
N/A

Numeric Literals

Sample Description
12345 Integer
0b1101 Binary
0×698def Hexadecimal (Hex)
0678 Octal
524.01 Float
.7361E-5 Scientific notation

 

 

 

 

 

 

 

Bitwise Operator Result Table

$v1 ^ $v2 $v1 & $v2 $v1| $v2 $v1 $v2
0 0 0 0 0
1 0 1 0 1
1 0 1 1 0
0 1 1 1 1

String Literals

Escape Character Description (ASCII Name)
\t Tab
\n Newline
\r Carriage return
\f Form feed
\b Backspace
\a Alarm/bell
\e Escape
\033 Octal character
\xff Hexadecimal character
\c[ Control Character
\l Next character is converted to lowercase
\u Next character is converted to uppercase
\L Next characters are converted to lowercase until \E is found
\U Next characters are converted to uppercase until \E is found
\Q Backslash all following non-alphanumeric characters until \E is found
\E Ends uppercase or lowercase conversion started with \L or \U
\\ Backslash character

Variables

NOTE: Variables in perl start with either $, @ or %

Declaration Meaning
$v Scalar variable
\$v Reference / memory address of $v.

 

$mf = \$v;
print $$v;

@a Array variable.

 

my @a = ( 'a', 'b', 'c' );
my @ma = ( [ ‘a’, ‘b’, ‘c’ ], [ ‘d’, ‘e’, ‘f’ ], [ ‘g’, ‘h’, ‘i’ ] );
print $#ma;  # Print last index of array.  +1 = row count.
print $#[0]; # Print number of columns in first row.

# Array of Hashes
my @aoh = (
{k1 => ‘v1′, k2 => ‘v2′, d => ‘v3′},
{k1 => ‘v4′, k2 => ‘v5′, d => ‘v6′},
{k1 => ‘v7′, k2 => ‘v8′, d => ‘v9′},
);

# Assign value to a member
$aoh[0]{’k1′} = “v10″;

\@a Reference / memory address of array @a.
print @$a;
print $$a[0];
print $a->[0];
%h Hash.  KEY / Value pair.

 

%h = (’user’ => ‘peanut’, ‘pass’ => ‘elephant’);

# Hash of Arrays
%hoa = (
k1 => [ “v1”, “v2” ],
k2 => [ “v3”, “v4”, “v5” ],
k3 => [ “v6”, “v7”, “v8” ],
);

# Assign value to a member
$hoa{k1}[0] = “v9″;

\%h Reference / memory address of hash %h.  IE:
print %$h;
print $$h{KEY};
print $p->{KEY};

String Operations

Code Description
$s1 . $s2 Concatenate strings $s1 and $s2
$s1 x $v1 Repeat string $s1 $v1 times
substr ( $s1, $pos, $len ) Get string starting at position $pos (NOTE: Positions start with 0) and extending to positition $pos+$len of string $s1.
index ($s1, $s2) Position where $s2 appears in $s1.
length(EXPR) Usually length($s1) where this function returns length of a string.  If string is “a” then length of it is 1.
rindex ($s1, $subs1, POSITION ) Position of last occurrance of $subs1 in $s1.  Starts at end of string unless POSITION is specified.
chr (ASCII VALUE) Character from ASCII value NUMBER.
lc($s1) Convert all alphabetic chars in $s1 to lowercase.
uc($s1) Convert all alphabetic chars in $s1 to UPPERCASE.

Built In Literals

Literal Description
__LINE__ Represents the current line number
__FILE__ Represents the current filename
__END__ Represents the logical end of the script; trailing garbage is ignored.
__DATA__ Represents a special filehandle
__PACKAGE__ Represents the current package; default package is main

Control Blocks

Control Structure Description / Example
if ( …CONDITION… ) {

 

print “IF\n”;
} elsif ( …CONDITION… ) {

print “ELSIF\n”;

} else {

print “ELSE\n”;

}

Standard perl if / elsif / else control structure.  Conditions can be anything, for example, ( $v1 <= $v2 ) etc
unless ( …CONDITION.. ) {

 

print “UNLESS\n”;

} elsif ( …CONDITION… ) {

print “ELSIF\n”;

} else {

print “ELSE\n”;

Opposite effect in initial statements to if.  Essentially equals: if ( ! … . Following elsif and else have the same reverse effect.  Use at your own risk.
while ( …EXPRESSION… ) {

 

print “Doing something..!”;

}

OR

do {

} while ( …EXPRESSION… );

First example test is done before loop start.  Second test on EXPRESSION is done after loop runs 1 iteration.
until ( …EXPRESSION… ) {

 

print “Doing something..!”;

}

OR

do {

} until ( …EXPRESSION… );

Evaluate until TRUE or ZERO.
for ( EXP1; EXP2; EXP3 ) {

 

}

EX:

 

for ( my $i = 0, my $j = 5, my $k = 9; $i < 100; $i++ ) {
print “You will see me 100 times.\n”;
…do something with $j…
…do something with $k…
}

foreach (…EXPRESSION…) {

 

}

EX: foreach $h(1..24) { …print $h”\n”; }

 

EX: foreach $str(’abc’, ‘def’, ‘ghi’, ‘jkl’) { print $str”\n”; }

EX: foreach (1..10) { print $_”\n”; }  # Uses perl assumed default variable $_ if no explicit variable is specified.

EX: foreach $s1(@numbers) { print $s1″\n”; }  # NOTE: Anthing doe to $s1 effects the corresponding value in @numbers array however @numbers is not modified outside the scope of foreach.

EX: foreach (@lucky) { if (….) { print “You’re lucky!\n”; }

LABEL: {

 

.

.

redo LABEL unless $1 == $2;

}

Similar in many ways to BASIC goto statement.  Also flow control.
NO switch / case statement in Perl. NO such thing in Perl.  But, alas, we can play pretend dressup and simulate one using labels:

 

LABEL: {
$v1 > 5 && do {
print “v1 > 5\n”;
last LABEL;
};
$v1 > 4 && do {
print “v1 > 4\n”;
last LABEL;
};
$v1 > 3 && do {
print “v1 > 3\n”;
last LABEL;
}
}

goto TAG

 

.

.

.

TAG:

Yes, this relic exists in Perl.  It is also created with labels.

 

goto LABEL
.
.
.
.
LABEL:

This is good for testing code.  (IE when you want to skip over some code in your script so you can test a newly created module.)

Loop Flow Control Modifiers

Keyword Effect Example
last Equivalent to break in C.  Leaves / exits the innermost enclosing loop / block. while (1) {  # Infinite loop.
.
.
last if $v1 == 45;
.
.
}
continue Cause while loop to function as a for statement.  continue adds an enclosing block inside a while loop. while ( $v1 <= 100 ) {
print “Do something with $v1…\n”;
.
.
next;
.
.
} continue { $v1++; }

 

# NOTE: The next statement normally causes control to go back to the top of the while loop, the while (…) statement.  In this case, because continue is used, next passes control to the continue block.

redo Repeats loop block without evaluating the loop expression again. LABEL: {
.
.
redo LABEL if ( …CONDITION… );
}
next Skip to next iteration of innermost enclosing {}. NOTE: See continue example above for next example.

Printing Format Specifiers (sprintf, printf etc)

Formatter Description
%b Unsigned binary integer
%c Character
%d, i Decimal number
%e Floating point number in scientific notation
%E Floating point number in scientific notation using capital E
%f , %F Floating point number
%g, %G Floatin gpoint number using either e or f conversion, whichever takes the least space.
%ld, %D Long decimal number
%lu, %U Long unsigned decimal number
%lo, %O Long octal number
%p Pointer (hexadecimal)
%s String
%u Unsigned decimal number
%x Hexadecimal number
%X Hexadecimal number using capital X
%lx Long hexadecimal number
%% Print a literal percent sign
%V Interpret as a Perl standard integer type
%l Interpret integer as a C type ‘long’ or ‘unsigned long’
%h Interpret integer as a C type ’short’ or ‘unsigned short’

Printing Specifier Modifiers

Format Modifiers Description
%- Left-justify within the given field width; Right justification is the default
%# Print leading 0 if printing octal; Print leading 0x if printing hexadecimal
%+ For conversions using d, e, f, and g, integers are dislayed with a numeric sign, + or –
%0 Pad displayed value with zeros instead of spaces (whitespace)
%number Field width / column to print in.  IE: printf “%6d”, 456; prints “   456″ where as printf “%d”, 456; prints “456″
%.number Floating / Decimal precision.  IE: printf “%.3f”, 0.72625183; prints “0.726″.  printf “%4.2f”, 23.538; prints “  23.53″.  Total space of last example is 7 (4 for left side, 1 for ‘.’. and 2 for decimal value)
.* Integer number before the actual argument that has to be formatted is used as the precision

Alternative Quoting

Code Description
q/Hello/ ‘Hello’
qq/Hello/ “Hello”
qx/date `date`
@list=qw/red yellow blue/; @list=( ‘red’, ‘yellow’, ‘blue’ );

Pack / Unpack Modifiers

IE: This table is used to specify the type of char and how many chars will be formatted.

TEMPLATE Detail
a Binary string, null padded
A ASCII string, space padded
b Bit string. Ascending bit order in each byte
B Bit string.  Descending bit order in each byte
c Signed char (8-bit value)
C Unsigned char (octet value)
d Double precision float in local/native format
f Single precision float in local/native format
h Hex string (Low nybble first)
H Hex string (High nybble first)
i Signed integer
I Unsigned integer
l Signed long (32-bit)
L Unsigned long (32-bit)
n 16-bit integer (short) in “network” (big-endian) order
N 32-bit integer (long) in “network” (big-endian) order
p Pointer / memory address to a structure (fixed length string)
P Pointer / memory address to null terminated string
q Signed Quad (64-bit)
Q Unsigned Quad (NOTE: Available only in x64 systems and x64 compiled Perl.  Error thrown otherwise
s Signed short (16-bit)
S Unsigned short (16-bit)
u A uuencoded string.
v 16-bit integer (short) in “VAX” (little-endian) order
V 32-bit integer (long) in “VAX” (little-endian) order
w A BER compressed integer whos bytes represent an unsigned integer in base 128, MSD first, in least number of digits.  Bit eight (the high bit) is set on each byte except the last
x null byte
X Back up a byte
@ Pad / truncate with nulls to absolute position
. null fill or truncate to absolute position specified by value
( Start of a ()- group
Z ASCIZ string, null padded

PERL REGULAR EXPRESSIONS

Optional Delimeters

Expression Description Example
/Reg. Expr./ Default delimeter is / /SearchString/
m#Reg. Expr.# Delimeter is #.  Operator m used to specify it. m#/usr/var/adm#
m{Reg. Expr.} Delimeter is {.  Operator m used to specify it. m{SearchString}

Operators, Matching Modifiers

NOTE: The m, s and x options are defined in Perl 5 only.

Modifier Description Substitution Modifier OR Matching Modifier Example
e Treat replacement side as an expression. SM s/6/6 * 7.3/eg
i Case insensitive match SM / MM s/string/replace/i

 

print if /string match/i;

m Consider string as multiple lines SM / MM  
o Optimize search.  Compile pattern once only SM / MM s/string/replace/og
s Treat a glob of text with \n embedded as a single string SM / MM s/string/replace/sg
x Ignore whitespace and allow comments in reg. expr. SM / MM s/string/replace
# This is a comment.
/x
g Global match for ALL occurrances of specified pattern.   For arrays, it returns a list.  For scalars it returns TRUE / FALSE. SM / MM s/string/replace/g

Operator Table

Operator Description Example
tr Translate characters found. $v1 =~ tr/a-z/A-Z/;
s Used for substitution s/string/replace/g
qr Match only once.  Like m/../. qr/SearchString/imosx
y Replace characters of search pattern with corresponding characters from second pattern. y/p1/q2/d

‘y’ Modifier Table

Modifier Description Example
c Commplement of ‘p’ y/p1/q2/c
d Delete found but not replaced chars. y/p1/q2/d
s Clobber duplicate replaced chars. y/p1/q2/s

Perl REGEX Metacharacters

Metacharacter Meaning
Digits  
. Any character except newline. Equivalent to [^\n]
[a-z0-9] Any single character in set
[^a-z0-9] Except any character in set
\d Matches one digit
\D A non-digit.  Equivalent to [^0-9]
\w Alphanumeric (dictionary word) character
\W Non alphanumeric (non word) character.
Character Repetition  
x? Matches 0 or 1
x* Matches 0 or more occurrences of x
x+ Matches 1 or more occurrences of x
(xyz)+ Matches 1 or more patterns of xyz
x{m,n} Matches at least  m occurrances of x and no more then n occurrances of x
(xyz)? Match 0 or 1 occurrences of xyz
(xyz)* Match 0 or more occurrences of pattern xyz
Curbing Greediness  
x?? Match 0 or 1 of x
(xyz)?? Match 0 or 1 of xyz
x*? Match 0 or more of x
(xyz)*? Match 0 or more of pattern xyz
x+? Match 1 or more of x
(xyz)+? Match 1 or more of pattern xyz
x{m,n}? Match at least m but no more then n of x
x{m}? Match at least m of x
x{m,}? Match m times
Unicode,ASCII,Control Chars and others  
\12 Matches octal value, up to 377
\x811 Match hex value
\cX Match control character.  IE: \cC is CTRL-C (Copy), \cV is CTRL-V (Paste)
\e Match ASCII ESC char
\E Case change end marker
\l Convert next character to lowercase
\L Match lowercase characters until the end of string or until \E encountered
\N Matches named unicode character.  IE \N{greek:Gamma}
\p{PROPERTY} Match char with named  PROPERTY.  IE \p{isBeta}
\P{PROPERTY} match any char w/o the named property.
\Q Quote meta chars until \E
\u Titlecase next one char.
\U Convert to uppercase all chars that can be until \E encountered
\x{N} Match Unicode N provided in hex
\X Match Unicode “combining character sequence” (D17) string
unicode.org:
D17 Combining character sequence: A character sequence consisting of either a base character followed by a sequence of one or more combining characters, or a sequence of one or more combining characters.
\[ Matches that metacharacter
\\ Match backslash
Whitespace  
\s Match whitespace: tab, space, newline, etc
\S Match NON-whitespace: anything other then what \s matches
\n Match newline
\r Match carriage return
\t Match tab
\f Match formfeed
\b Match backspace
\0 Match null character
Anchors  
\b Match word boundary when not used inside [ ]
\B Match NON-word boundary when not used inside [ ]
^ Match start of line
$ Match end of line
\A Match beginning of string only
\Z Match end of line/string
\z Match end of string only
\G Match where previous m//g left off
REGEX variables  
(string) Used for referencing / assigning a value to the subsequent regex variable: $1, $2, $4 … .
\1 or $1 Match return value inside first set of () found in regex
\2 or $2 Match return value inside second set of () found
\3 or $3 Match return value inside third set of () found
\4 or $4 Same as above and so on and so on ….
Bracketed Class  
[:alnum:] Alphanumeric
[:alpha:] Alphabetic
[:ascii] Chars with value between 0 – 127 inclusive
[:cntrl:] Control chars
[:digit:] Numeric chars
[:graph:] Non-blank char (whitespace) other than alphanum or punctuation chars.
[:lower:] Lowercase alphabetic chars
[:print:] Like [:graph:] but includes space char
[:punct:] Punctuation chars
[:space:] Whitespace like \s
[:upper:] Uppercase alphabetic chars
[:word:] Alphanumeric or underline char
[:xdigit:] Hexadecimal digit numbers like [0-9a-fA-F]
Look ahead/behind  
/PATTERN(?=pattern)/ Positive look ahead
/PATTERN(?!pattern)/ Negative look ahead
(?<=pattern)/PATTERN/ Positive look behind
(?<!pattern)/PATTERN/ Negative look behind
utf8 Char Classes  
\p{IsASCII} ASCII Character
\p{Cntrl} Control character
\p{IsDigit} A digit: 0 – 9
\p{IsGraph} Punc. or Alphanum. char
\p{IsLower} Lowercase letter
\p{IsPrint} Alphanum., punct. or space
\p{IsPunct} Punct. char
\p{IsSpace} Whitespace char
\p{IsUpper} Uppercase letter
\p{IsWord} Underscore or alphanum. word.
\p{IsXDigit} Hex digit.

Perl File I/O

NOTE: (#R = Reading, #W = Writing, #A = Append, #P = Pipe)

Code Example
OPEN open(FILEH, “FILE.xyz”);  # R
open(FILEH, “<FILE.xyz”);   # R
open(FILEH); # R
open FILEH;
open(FILEH, “>FILE.xyz”);  # W
open(FILEH, “>> FILE.xyz”); # A
open(FILEH, “| more”); # P

 

Tag Description
> Overwrite
< Read
>> Append
+< Read then write
+> Write then read
+>> Append then read
| <SYS COMMAND> Pipe output to another command
<SYS COMMAND> | Pipe input from another command
CLOSE close(FILEH);
close FILEH;
R/W my @lines=<FILEH>;  # R
while(<FILEH>) {  # R

}
while($line = <FILEH>){   # R

}
print FILEH “Goodbye World! If ‘>FILE.xyz used, file overwritten.  If ‘>> FILE.xyz’ used, this string would then be appended to end of file.\n”;  # W / A

 

# Declare files as binary (Use after ‘open’)
binmode ( FILEH_IN ); # R (binary read)
binmode ( FILEH_OUT );  # W (binary write)

# Used after ‘binmode‘ is called on file handle
while ( read(FILEH_IN, $buffer, 1024) ) {  # R (binary)
#… read 1024 bytes from FILEH_IN and store in $buffer…
}

# Redirect STDOUT to a file instead of terminal
open(BACKUP, “>&STDOUT“);
open(STDOUT, “>$FILE.xyz”);
.
.
open(STDOUT, “>&BACKUP“);

FUNCTIONS select (FILEO);  # Select FILEO as default.  print “abc”; would then print to file FILEO instead of STDOUT.

 

flock(FILEH, $lockType);  # See next table below for file lock types.
flock(FILEH, 2);  # Lock file
flock(FILEH, 8);  # Unlock file

seek(FILEH, BYTE_OFFSET, FPOS); # Move read pointer in file to FPOS position:

’seek’ FPOS (seek start location

0 Start
1 Current
2 Last (End)

tell (FILEH);  # Return current byte position in file
tell; # Return current byte positio of last file read.

eof (FILEH); # Check if EOF reached
eof;
print if /stringfound/ .. eof() # Print all lines between ’stringfound’ and EOF to FILEH.  ‘..’ is range operator.

DEFAULT HANDLES STDOUT
STDIN
STDERR

Perl File Lock Types

Name Value Description
lock_sh 1 Shared lock
lock_ex 2 Exclusive lock
lock_nb 4 Non blocking lock
lock_un 8 Unlock existing lock

Script Arguments

Name Description
ARGV Argument list Filehandle
$ARGV Scalar
@ARGV Array
$0 Current script name
$#ARGV Argument list

File Test Operators
EXAMPLE: print if -r $file;  # Print if file is readable.
if ( -x $file ) { # Run code if file is executable
}

Flag Tests Description
-r Readable
-w Writable
-x Executable
-o Owned by UID of running script
-e Exists
-z Zero in size
-s Non zero size.  Returns file size (bytes).
-f Plain file
-d Directory
-l Symbolic link
-p Named pipe or FIFO
-S Socket
-b Block file
-c Char special file
-u SETUID bit set.
-g SETGID bit set.
-k Sticky bit set
-t TTY (And open)
-T Text file
-B Binary file
-M Returns days # since last modified
-A Returns day # since last accessed
-C -||- since inode changed

Simplified Cheat Sheet can be found on the Perl site.

Leave a Reply

You must be logged in to post a comment.


     
  Copyright © 2003 - 2013 Tom Kacperski (microdevsys.com). All rights reserved.

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License