Descriptive cheat sheet
|
Logical
|
or, and, xor, not, ||, &&, !
|
|
Strings
|
eq, gt, lt, le, ge, cmp
|
|
Numbers
|
==, <,>,<=,>=,<=>
|
|
Whitespace
|
\s
|
|
word
|
\w
|
|
Digit
|
\d
|
|
Non digit
|
\D
|
|
Beginning
|
^
|
|
End
|
$
|
|
min, max
|
\d{min, max}
|
|
7 to 11 digit
|
\d{7,11}
|
|
Only matches 7 digit
|
\d{7}
|
Sub setenv{
my ($key, $value)=@_;
$ENV{$key)=$value unless $ENV{$key}
}
BEGIN {
my @scale = ("A" .. "G");
my $note = –1;
sub next_pitch { return $scale[ ($note += 1) %= @scale ] }; }
for $file(@names){
my $fh;
open($fh, $file) || next;
$filehandle{$file}=$fh;
}
$scalarref= *foo{SCALAR} # \$foo
$arrayref=*ARGV{ARRAY} # \@ARGV
$hashref = *ENV{HASH}; # \%ENV
$coderef=*handler{CODE} # \&handler
$gobref=*foo{GLOB}
$ioref=*STDIN{IO};
$format=*foo{FORMAT};
use strict “refs”
our $value = "global"; {
my $value = "private";
print "Inside, mine is ${value}, "; say "but ours is ${"value"}.";
}
say "Outside, ${value} is again ${"value"}.";
|
q//
|
' '
|
|
qq//
|
" "
|
|
qx/ /
|
Command exec
|
|
qw//
|
WordList
|
|
m//
|
Pattern match
|
|
s//
|
Substitute
|
|
y///
|
Translation
|
|
Qr//
|
Regular Expression
|
@tailings=popmany(\@a, \@a2, \@a3);
Sub popmany{
my @retlist=()
for my $ref (@_) {
push @retlist @$ref;
}
return @retlist;
}
%table= (
'john' => {age =>47, eyes=>'brown', weight=>186},
'Mary'=>{age=>23, eyes=>'hazel', weight=>128}
);
print $table{john}{age}
$name=”bam”;
$$name=1; #sets $bam
$name->[0]=4;
#set the first element of @bam
$name->{X}=”Y” #sets the X element of Y
@$name=() #resets the @bam
&$name #calls the function bam
|
@stuff=(“one”, “two”, “three”)
|
Assign entire list to array
|
|
$stuff=(“one”, “two”, “three”)
|
“three” is assigned to stuff
|
|
$stuff=@stuff;
|
$stuff is assigned to length of the array i.e 3
|
|
@releases=(“alpha”, ”beta”, ”gamma”,)
|
Comma is allowed in the last.
|
|
$modificationtime=(stat($file))[9];
|
Modification time
|
|
$hexdigit=(“a”,”b”,”c”,”d”,)[$digit-10]
|
hexdigit
|
|
($day, $month, $year)=(localtime)[3, 4,5];
|
day/month/year
|
|
@days + 0;
|
implicit forces array into scalar
|
|
scalar(@days)
|
explicit r @days into scalar
|
|
@whatever=()
|
$#whatever=-1
|
|
undef(@whatever)
|
recover its memory
|
|
scalar(@whatever)
|
$# whatever + 1;
|
|
print “-“ x 80
|
prints row of dashes.
|
|
my @ones = (1) x 80;
|
list of 80 1’s;
|
|
@ones = (5) x @ones
|
#set all elements to 5
|
|
my %hash
|
@hash(@keys) = (“ “) x @keys;
|
|
my @keys
|
@keys=qw( perl doc yyy);
|
@reflist = ( \$s, \@a, \%h)
Is equal to
@reflist=\($s, @a, %h)
@reflist=\(@x);
Is equal to
@reflist=map(\$_}@x;
@reflist = \fx();
Is equal to
@reflist=map{\$_} fx();
@reflist=\(fx(), fy(), fz());
Is equal to
@reflist=map{\$_} fx(), fy(), fz();
@reflist=\localtime(); #ref to each of nine time elements
$lastref = \localtime() #ref to whether it’s daylight saving time
$$ref is scalar value of $ref refers to
@$ref is array values of $ref refers to
%$ref is hash values of $ref referes to
$foo = “three humps”;
$scalarref = \$foo;
$camel_model= $$scalarref;
push(@$arrayref, $filename);
$$arrayref[0]=”Janualry” ; set the first element of @$arrayref;
@$arrayref[4…6] = qw /may June July/;
%$hashref = (KEY=>”RING”, BIRD=>”Sing”)
$$hashref{KEY}=”VALUE”;
@$hashref{“KEY1”, “KEY2”}=(“VAL1”, “VAL2”);
$refrefref=\\\”hody”;
print ($$$$refrefref);
my %map=(“aa”, 1, “bb”, 2);
or
my %map=(aa=>1, bb=>2,);
or
$map{aa}=1;
$map{bb}=2;
Reference:
$map={ “aa”. => 1, “bb”=>2 }
ls; Interpreted in shell and store the result in $k@files =<*.xml>
To
Filehandle in a variable
<$foo>
@files=glob(*”.xml”);
while(glob *.c){
chmod 0644 $_
}
@files=gob(“$dir/*.[ch]”);
|
Name(LIST)
|
& is optional with parameter or Name LIST
|
|
Name
|
exposes current @_ to that subroutine
|
Sub razzle{
print “Ok”
}
sub FLAG_FOO () { 1 << 8 }
sub FLAG_BAR () { 1 << 9 }
sub FLAG_MASK () { FLAG_FOO | FLAG_BAR }
sub OPT_GLARCH(){
{ (0x1B58 & FLAG_MASK) == 0 } {
{ return 23 }
{ return 42 }
}
Sub hashem{ +{@_} }
Sub hashem{ return{@_}}
%table= (
'john' =>[47, 'brown',186],
'mary '=>[23, 'hazel', 128]
);
print @{$table{john}}[0];
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.