10stripe's Perl Cheat-Sheet
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)
-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
%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
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
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
@ Null-fill to absolute position