prefix indicates variables type, literals don't need apost !but depends on context
Contexts sigils*
void $scalar the entire @array %hash
scalar @array a slice @array[0, 2] @hash{'a', 'b'}
list %hash one element $array[0] $hash{'a'}
&sub # elements scalar @array keys %hash
*glob last index $#array
Scalar Values number, string, reference, glob, undef
length $string
@array[ 1 .. 3 ] range
@array = splice @array, 1, 3 removes entries 1 and 3
$array[-1] === $array[last index] …
%hash= ( key , value , key , value );
$hash{ addkey } = "description" ;
$hash{ key }
@names = key %hash; $count = keys %hash
delete $hash{ key }; $hash{ key } = udef;
\ references $$foo[1] aka $foo->[1]
$@%&* dereference $$foo{bar} aka $foo->{bar}
[] anon. arrayref ${$$foo[1]}[2] aka $foo->[1]->[2]
{} anon. hashref ${$$foo[1]}[2] aka $foo->[1][2]
\() list of refs
Operator Precedence | Numbers | vs | Strings
|
->
++ --
**
! ~ \ u+ u-i
=~ !~ regexp
* / % x
+ - . | + | | .
|
== != | | eq ne
|
< > <= >= | | lt gt le ge
|
<=> | | cmp
|
+= -= *= /= | | .=
|
0, '0', '' , the empty list () , and undef are false
for (LIST) { }, for (a;b;c) { }
<< >> while ( ) { }, until ( ) { }
named uops if ( ) { } elsif ( ) { } else { } x=y if defined y;
< > <= >= lt gt le ge unless """""
== != <=> eq ne cmp for equals foreach (@array or l,i,s,t)
& =~ m/ match =~ s/ substitute
| ^ REGEX Metachars REGEX Modifiers
&& ^ string begin i ignorecase
|| $ str. end (before \n) m line based ^$
.. ... + at LEAST one s includes \n
?: * zero or more x ignore white space
= += -= *= etc. ? zero or one g global
, => {3,7} inclusive range o cmpl patt once
list ops () capture to $1 REGEX Charclasses
not (?:) no capture . == [^\n] ?(_)?
and [] character class \s == [' '\f\t\r\n] whitespace
or xor | alternation \w == [A-Za-z0-9_] word includes digits and uscore
\b word boundary \d == [0-9] \D == ![0-9]
\z string end \S, \W negate
.? lazy, ! greedy
*? lazy ( )+ a bunch of nbsp (not capture (_)?
xxyy\d+ := xxyy followed by a bunch of digits ex: xxyy001, xxyy1
while ( <>) { <> (STDIN) returns FALSE at EOF
chomp; #ditch \n <DATAFILE>
@array = split(/:/);
$field1 = $array[0];
Function Returns
stat localtime caller SPECIAL VARIABLES
0 dev 0 second 0 package $_ default variable, foreach iterator variable
1 ino 1 minute 1 filename @ARGV command line args
2 mode 2 hour 2 line @_ subroutine args
3 nlink 3 day 3 subroutine $1 … 1st&,hellip; match =~ ( regexp )
4 uid 4 month-1 4 hasargs $0 program name
5 gid 5 year-1900 5 wantarray $! sys/libcall error open: 2 "No such file or directory", 13 "Permission denied"
6 rdev 6 weekday 6 evaltext $@ eval error
7 size 7 yearday 7 is_require $$ process ID
8 atime 8 is_dst 8 hints $. input line number
9 mtime 9 bitmask $/ $\ input output separator
10 ctime just use %ENV environment
11 blksz POSIX:: 3..9 only more at English
12 blcks strftime! with EXPR DBI_DSN, DBI_DRIVER, DBI_USER, DBI_PASS
if defined
qw( abc def ) quot words
shift / unshift 1st element / add to start
pop / push last element / add to end
Do
use strict; Don't
use warnings; "$foo"
my $var; $$variable_name
open() or die $!; `$userinput`
use Modules; /$userinput/
search path @INC (@INC contains:
/usr/lib/perl5/site_perl/5.8.*/i386-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.*
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.*/i386-linux-thread-multi
/usr/lib/perl5
/vendor_perl/5.8.5/i386-linux-thread-multi
/usr/lib/perl5/vendor_perl/5.8.*
/usr/lib/perl5/vendor_perl
/usr/lib/perl5/5.8.8/i386-linux-thread-multi
/usr/lib/perl5/5.8.8 .
LINKS
This was adapted from perldoc.perl.org/perlcheat.html