Skip to content

Instantly share code, notes, and snippets.

@zserghei
Last active December 15, 2015 13:29
Show Gist options
  • Save zserghei/5267412 to your computer and use it in GitHub Desktop.
Save zserghei/5267412 to your computer and use it in GitHub Desktop.
Useful Perl commands

Predefined variables

$$ # process id
$_ # default variable, some functions use it, when nothing else is defined
$! # last error of OS (usefull with 'die')
@_ # list of arguments of function $_[0] - 1st argument, $_[1] -2nd argument
$~ # format for current descriptor
$% # number of pages (counter) for current descriptor
$- # number of lines (up-down counter) till the end of page for current descriptor
$^ # format of top for current descriptor
$= # number of lines per page for current descriptor

Numbers

  • dec: 12, -2, 7.2e45, -1.2E-23
  • oct: 012
  • hex: 0x12, 0X12

Escape symbols

\n   new line
\r   return
\t   tabbing
\f   form-feed
\b   backspace
\a   signal
\e   escape
\007 oct ascii code (here - signal)
\x7f hex ascii code (here - delete)
\cC  Ctrl + C
\\   \
\"   double quote # "
\l   next letter to lower
\L   all letters to lower till \E
\u   next letter to upper
\U   all letters to upper till \E
\Q   surround with \ all non-alphanumeric symbols till \E
\E   end of \L, \U, \Q

Operations with numbers

  • Arithmetic:
$x + $y;
$x - $y;
$x * $y;
$x / $y;
$x % $y;
$x ** $y; # x powered by y
  • Logical: <, <=, ==, >=, >, !=

Operations with strings

'xxx\'xxx \ntext\\'	#
"text\ntext \177"	# may contain escape symbols
$c = $a . $b		# concatenation, $a and $b are unchanged
$c = $a x $b		# repeating, $a - string, $b - number. $c = $b copies of $a
length (expr);		# returns length of scalar expression in bytes
lc (expr);	    	# to lower case
uc (expr);		    # to upper case
lcfirst (expr);		# first symbol - to lower
ucfirst (expr);     # first symbol - to upper
eval (expr);        # consider expr as text of perl-program and execute it
eq (==), ne (!=), lt(<), gt(>), le(<=), ge(>=)    # compare

Priority

->         # operations with lists (right to left)
->   ->    # method calling
--   ++, --
<-   **    # power
<-   !, ~, \, +, -    # logical not, bitwise not, reference, unar +, unar -
->   =~, !~           # equals, does not equal with regexps
->   *, /, %, x       # mult, div, mod, repeat
->   +, -, .          # add, sub, concatenate
--                    # unar operations
->   &                # bitwise and
->   !, ^             # bitwise or, bitwise xor
->   &&               #
->   ||               #
--   .., ...          # e.g. foreach (1..10) { } 
<-   ?:               # if-then-else
<-   =, +=, -=, *=, etc.
->   , =>
--                    # operations with list (left to right)
<-   not              # logical
->   and              # logical
->   or, xor          # logical

Scalar variables

$name = 5;
$x = chop($y); # deletes last symbol in $y and puts it in $x
chomp ($y);    # deletes last '\n' from $y

Scalars inside strings

$a = "xyz";
$b = "text $a text";  # $b = "text xyz text";
$b = "text ${a}text"; # $b = "text xyztext";
$c = "$a";
$b = "text $c";       # b = "text $a";    "\$" --> $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment