Perl enables you to write powerful programs right from the start, whether you’re a programming novice or expert. Perl offers the standard programming tools — comparison operators, pattern-matching quantifiers, list functions — and has shortcuts for inputting character ranges. Perl also offers file tests so you can find what you want fast. A perl regular expression usually comes in something like this: Here we divide the expression into 4 parts: = Match Operators, the operator between the variable and the expression m// Quote-like Operators, appears after match operator /i Options, the modifiers after the expression PATTERN the Expression Match Operators = This operator appears between the string. Operators to determine whether a file or directory exists Here are the most common Perl file test operators that can be used to determine whether a file or directory exists: -e File or directory name exists -z File exists and has zero size -s File or directory exists and has non-zero size. Perl Cheat Sheet Edit Cheat Sheet Installing Modules from CPAN. Invoke CPAN shell. Perl Cheat Sheet. This is a quick and dirty cheat sheet. Global Scalar Variables. Default scalar $. Integer Operators. Equal to less than.
As a companion to our earlier regular expression cheat-sheet, we have put together a general cheat-sheet for various parts of the Perl programming language. Perl is a big, big language, so of course we couldn't cover the entire language. But we did condense down some of the most frequently needed, hardest to memorize parts.
This is not a comprehensive overview of Perl and it will not teach you all of Perl. If you are completely new to Perl, we would suggest using this as a supplement to Learning Perl by Schwartz et al (buying from Amazon helps support 10stripe).
If you would like a print version, we strongly recommend the PDF version of this page. We have made some adjustments so that printing this HTML document should also be a workable solution (results will look best in a modern browser with strong CSS support). It won't be quite as nice as the PDF version.
Bitwise Operators
& And
| Or
^ XOR
<< Shift left (Usage: 6 << 2 shifts left 2)
>> Shift right
~ Bitwise negation
File Tests
Usage: if (-r $filename) {}
-r Readable by this (effective) user/group
-w Writable by this (effective) user/group
-x Executable by this (effective) user/group
-o Owned by this (effective) user/group
-R Readable by this real user/group
-W Writable by this real user/group
-X Executable by this real user/group
-O Owned by this real user/group
-e Exists
-z Exists and has zero size (false for directories)
-s Exists and nonzero size (returns size in bytes)
Download free divx player for mac. -f Is a plain file
-d Is a directory
-l Is a symbolic link
-S Is a socket
-p Is a named pipe ('fifo')
-b Is a block-special file (like a mountable disk)
-c Is a character-special file (like an I/O device)
-u Is a setuid
-g Is a setgid
-k Has sticky bit set
-t Is a tty
-T 'Looks like' plain text (reads beginning of file)
-B 'Looks like' binary file (reads beginning of file)
-M Time since last modified (floating-point days)
-A Time since last accessed (floating-point days)
-C Time since inode last modified (floating-point days)
printf and sprintf Formats
%c Character with the given number
%s String
%d Signed integer, in decimal
%u Unsigned integer, in decimal
%o Unsigned integer, in octal
%x Unsigned integer, in hexadecimal
%e Floating-point number, in scientific notation
%f Floating-point number, in fixed decimal notation
Download java 6 for mac but cant find. %g Floating-point number, in %e or %f notation (auto)
%% Literal percent sign
Variable Types and Sigils
$scalar Single value
@array List of values
%hash List of key/value pairs
*typeglob All types (holds $typeglob, @typeglob, etc.)
&subroutine Named block of instructions
$reference Memory address (or @reference, etc)
Flow Control
{} 'Naked' block for scope control
while (true) {} Loop while evaluates to true
until (false) {} Equivalent to while !(false) {}
if (true) {} Execute once if evaluates to true
Perl Comparison Operator
elsif (true) {} Follows if, note the missing ‘e’
else {} Follows if
unless (false) {} Equivalent to if !(false) {}
for (initial;test;iterate) {} Loop, e.g. for ($i=0;$i<9;$i++) {}
foreach (@items) {} Option: foreach $item (@items) {}
last Exit loop; like C’s break
next Advance loop to next iteration
Perl Operators Cheat Sheet Printable
redo Repeat loop, do not iterate
pack and unpack Templates
a Null-padded string of bytes
A Space-padded string of bytes
b Bit string, ascending bit order inside each byte (like vec)
B Bit string, descending bit order inside each byte
c Signed char (8-bit integer)
C Unsigned char (8-bit integer); see U for Unicode
d Double-precision (64-bit) floating point, native format
f Single-precision (32-bit) floating-point, native format
h Hexadecimal string, low nybble first
H Hexidecimal string, high nybble first
i Signed integer, native format
I Unsigned integer, native format
l Signed long, always 32 bits
n 16-bit short in 'network' (big-endian) order
N 32-bit long in 'network' (big-endian) order
p Pointer to a null-terminated string
P Pointer to a fixed-length string
q Signed quad (64-bit integer) value
Q Unsigned quad (64-bit integer) value
s Signed short value, always 16 bits
S Unsigned short value, always 16 bits
u A uuencoded string
U Unicode character number
v 16-bit short in 'VAX' (little-endian) order
V 32-bit long in 'VAX' (little-endian) order
w BER compressed integer
x Null byte (skip forward a byte)
X Back up a byte
Z Null-terminated (and null-padded) string of bytes
Perl Operators Cheat Sheet Template
@ Null-fill to absolute position