10stripe's Perl Cheat-Sheet

Document Sample
10stripe's Perl Cheat-Sheet Powered By Docstoc
					                                               10stripe’s Perl Cheat-Sheet
Variable Types and Sigils                                            Bitwise Operators
$scalar             Single value                                     &    And
@array              List of values                                   |    Or
%hash               List of key/value pairs                          ^    XOR
*typeglob           All types (holds $typeglob, @typeglob, etc.)     <<   Shift left (Usage: 6 << 2 shifts left 2)
&subroutine         Named block of instructions                      >>   Shift right
\$reference         Memory address (or \@reference, etc)             ~    Bitwise negation

Flow Control                                                         File Tests
{}                              "Naked" block for scope control      Usage: if (-r $filename) {}
while (true) {}                 Loop while evaluates to true         -r   Readable by this (effective) user/group
until (false) {}                Equivalent to while !(false) {}      -w   Writable by this (effective) user/group
if (true) {}                    Execute once if evaluates to true    -x   Executable by this (effective) user/group
elsif (true) {}                 Follows if, note the missing ‘e’     -o   Owned by this (effective) user/group
else {}                         Follows if                           -R   Readable by this real user/group
unless (false) {}               Equivalent to if !(false) {}         -W   Writable by this real user/group
for (initial;test;iterate) {}   Loop, e.g. for ($i=0;$i<9;$i++) {}   -X   Executable by this real user/group
foreach (@items) {}             Option: foreach $item (@items) {}    -O   Owned by this real user/group
last                            Exit loop; like C’s break            -e   Exists
next                            Advance loop to next iteration       -z   Exists and has zero size (false for directories)
redo                            Repeat loop, do not iterate          -s   Exists and nonzero size (returns size in bytes)
                                                                     -f   Is a plain file
pack and unpack Templates                                            -d   Is a directory
a    Null-padded string of bytes                                     -l   Is a symbolic link
A    Space-padded string of bytes                                    -S   Is a socket
b    Bit string, ascending bit order inside each byte (like vec)     -p   Is a named pipe ("fifo")
B    Bit string, descending bit order inside each byte               -b   Is a block-special file (like a mountable disk)
c    Signed char (8-bit integer)                                     -c   Is a character-special file (like an I/O device)
C    Unsigned char (8-bit integer); see U for Unicode                -u   Is a setuid
d    Double-precision (64-bit) floating point, native format          -g   Is a setgid
f    Single-precision (32-bit) floating-point, native format         -k   Has sticky bit set
h    Hexadecimal string, low nybble first                             -t   Is a tty
H    Hexidecimal string, high nybble first                            -T   "Looks like" plain text (reads beginning of file)
i    Signed integer, native format                                   -B   "Looks like" binary file (reads beginning of file)
I    Unsigned integer, native format                                 -M   Time since last modified (floating-point days)
l    Signed long, always 32 bits                                     -A   Time since last accessed (floating-point days)
n    16-bit short in "network" (big-endian) order                    -C   Time since inode last modified (floating-point days)
N    32-bit long in "network" (big-endian) order
p    Pointer to a null-terminated string                             printf and sprintf Formats
P    Pointer to a fixed-length string                                 %c       Character with the given number
q    Signed quad (64-bit integer) value                              %s       String
Q    Unsigned quad (64-bit integer) value                            %d       Signed integer, in decimal
s    Signed short value, always 16 bits                              %u       Unsigned integer, in decimal
S    Unsigned short value, always 16 bits                            %o       Unsigned integer, in octal
u    A uuencoded string                                              %x       Unsigned integer, in hexadecimal
U    Unicode character number                                        %e       Floating-point number, in scientific notation
v    16-bit short in "VAX" (little-endian) order                     %f       Floating-point number, in fixed decimal notation
V    32-bit long in "VAX" (little-endian) order                      %g       Floating-point, let Perl choose good way (%e or %f)
w    BER compressed integer                                          %%       Literal percent sign
x    Null byte (skip forward a byte)
X    Back up a byte
Z    Null-terminated (and null-padded) string of bytes
@    Null-fill to absolute position                                                      10stripe.com/go/perl

Composed by Alex Freeman for 10stripe.com. Based on Programming Perl by Larry Wall, Tom Christiansen, and Jon Orwant.