Perl Operators Cheat Sheet



Perl Regular Expression Quick Reference Card Revision 0.1 (draft) for Perl 5.8.5 Iain Truskett (formatting by Andrew Ford) refcards.comTM This is a quick reference to Perl’s regular expressions. For full information see the perlre and perlop manual pages. Operators =Ÿ determines to which variable the regex is applied. In its ab-sence, $is used. Floating-Point Types. Perl supports platform-native floating-point as scalar values; in practice this usually means IEEE 754 double precision. Perl can also store decimal numbers as strings, but the builtin arithmetic operators will convert them to integer or floating-point values to perform the operation. Perl Reference Card Cheat Sheet. This is version 2 of the perl reference card. The Python cheat sheet is a one-page reference sheet for the Python programming language. Our 4-page guide includes detailed information on: numbers and characters, operators, if/else command, arrays, switch statements, loops. Operators and precedence Perl operators have the following associativityand precedence, listed from highest precedence to lowest. Assoc Operators Description left terms and list operators See below. Left - Infix dereference operator. Auto-increment (magical on strings).- Auto-decrement. Right. Exponentiation.

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)

-f Is a plain file

Cheat

-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)

Perl Operators Cheat Sheet

-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

Perl

-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

Perl Comparison Operator

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

Perl Operators Cheat Sheet Template

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

Perl Assignment Operators

Z Null-terminated (and null-padded) string of bytes

Perl Operators Cheat Sheet

@ Null-fill to absolute position