PERL CHEAT SHEET:
Online Documentation:>perldoc perl

$name = scalar
our $var; #global variable
my $var; #local or lexical variable
(variables) = (items); #this will assign list of items to list of vriable
-()- = empty list
qw{ } is list creation operator
( ) = list - element numbers are zero based
(list)[n] - to access nth member of list - zero based
@name = array #basically a variable equal to a list
$num = @array; #this will assigne number of elements - not zero based!
print scalar (@array); # will perform the same thing
@array =(@array, 'w'); will add element w at end of array
$var = $array [n]; #this will access the nth value of array - zero based
print @array[n,m]; #prints nth and mth elements of the array
$var = $#array; this will get the last element number (zero based) in the array
%var is a hash variable which links pairs of elements
=> links hash elements and keys, other wise list with doube quotes
$var = $hash{key}; this gets the element

\ is the escape character
. concatenation operator
ord converts ascii to number
(...) range operator
>> bit shifting operation
<< bit shifting operation
=~ binding operator - get result of some regular expression operation returns true of regular expression matches any part of string
!~ negative binding operator - will be false if match is found
/expr/i is for case insensitive matching
m{} or m// is the matching operator
s{} {} is the substituion operator
tr{} {} is the transliteration operator
lt, gt, eq, ne, ge, le, cmp : string comparison operators, cmp is -1lt, 1gt, 0equal
<, >, ==, !=, <=, >=. <==> ;numeric comparison operators
reverse operator reverses order of elements in an array, it also switches elements with keys in hashs.
push and pop to add or remove last element in an array
shift and unshift to remove or add element at the start of an array.
sort() to sort a list or an array.
join function to join array of strings to single string with specified delimiter.

for (starting assignment;test condition;increment;) {code} # for statement
foreach $i (list, array, hash) {code;} #the #1 can be used to keep track of array number otherwise it can be left out.

########to read or write a file:
open (my $infile, "<", "NYPA_STANDARD_ELEMENTS_ESET.DDL") or die "Can't open eset $!";
print "ESET file opened\n";

while (<$infile>){code;} # change to ">" to write new file or ">>" to append to a file
The <,> and >> can also be used on command line to pass file names.
The pipe can also be used to pass the results from one program to another.

############

####get input from terminal
print "Enter data:";
$inpt = <STDIN>;
chomp ($inpt); ^#this is to remove \n
########

#####regular expressions are enclosed in forward slashed /expression/ and don't need quotes.
. is an character
* any number of characters
^ archors match to beginning of string
$ anchors match to end of the string

s/find/replace/; #will only do first instance
s/find/replace/g; #will do all the instances becuase og g global modifier
s/find/replace/m;#allows ^ and $ serches in each part of string seperated by a new line \n
s/find/replace/s; ignores new line characters
s/find/replace/x; ignores whitespace and allows comments in an expression
(?#comment) -this can be used to enclose comment in regular expression.
$1, $2, etc... are the stored result of the searches. these are called back references.

$=, ?<, are look aheads and look behinds

######DEFUALT VARIABLES
$_ - default variable when no variable is specifeid for assignement, anf for reading files.
@_ the variables passed to a subroutine and retrieved as $_[n]
@ARGV - array of variables pass from the command line
$ARGV - null file handle file name (??)
$& last successful matched string

############### use of split to parse strings
@inarray = split/delimiter/ ,$instr;
###############

##############REFERENCES
$ref=\$var; #creates reference. reference is scalar but can be to array or hash.
$reflist = [list items]; #here a reference is an alternate to a list???
$refhash = {a=>n, etc.} #this is called a referecnne to an annonomous hash
TO dereference, ie access referred object put proper variable type in front of curly brackets e.g. @{$ref}
Shorthand dereferencing involves $$, @$, %$ without the curley braces.
to reference an element you can use square brackets as in ${$ref}[n] - note that here we have $ in front for singel element.
alternate array reference is $var = $arrayref -> [n];
############

sub name{code;}
#####for prototype just name and passed types
subrt(\@\$); # subrt geta an array and a scaler
###to call just enter name();
use subs ("name1", "name2", "name3"); # to allow calling without ()
the return in the sub provides the result of the subroutine
subroutine prototypes can have variable types but don't need names eg - sub addarrays(\@\@); this indicated 2 arrays will be passed.
wantarray and wantscaler functions let subroutine know what is expected in return statement
sub routine parameters are stored by defualt in @_

####acess other files:
require "filename.plx"; #should have a different package name to allow overlap of variables and subroutines
local or my keeps variables locally scoped.
############

#####references are scalars that are addresses of other types
#####you can put them in arrays to create complex structures
####you create a reference using the \ character
$ref = \$scal;
$arrayref = \@array;
###ref operator returns type of reference of a referenc
print ref $ref;

######annommousr references to lists or hashes:
$listref= [list items];
$hashref = {keys and vlaues using => for each pair, pairs separated by commas or jsut even number of listed items];
#####to dereference an reference put in {} preceded by variable type, $, @ or % (see below for shorthand dereferencing
print @($arrayref); #prints the elements of the referenced array
$val = $($arrayref)[0];# sets $val equal to first element of aray
$val = $arrayref -> [1]; #short hnd notaion for 2nd member of referenced array
undef $ref; #deletes reference

####& is reference to a subroutine

####autovivification creats structure for data first time it is referenced
$ref1 -> ($ref2) ->[0] =7;#creates an array of arrays.
####the exists operator checks to see if data is empty or has bee populated.

 

#####Classes and objects are handled as references
#####the bless operator changes a referecne to the type of class desired.
#####bless(reference,package) where leaving ut package brings it into the current package.
#####class method has string as first variable in calling routine
#####object method has a reference as first parameter passed by calling routine
####Classes are declared as packages, saved as a file with a .pm extension
####To iuse other class methods type "use classname;"
####sub new { code; } #constructor code goes like this
####to create object use new and class name
####last statement in a class is 1; to tell perl that class object loaded into memebory successfully
our $obj = PackageName->new(passed variables);
@ISA = qw(Class to be inherited); #this is required for class methods

Tied variables allow variables to be used to access methods and properties of a class

####the @INC array provides the files that will be looked for in function or sub calls (use lib "path"; to add to array)
##### do - runs a modules, require - loads and runs file once, use - compile time run of file
#### modules are set up in files with pm extension usually one module per file
sub BEGIN {push @INC, "new path";} use modulename; ####adds new path to the @INC array, and names module in it
use lib "new path"; use modulenname; ####same thing
####standard perl modykes include File::Find (subroutines find and findepth), File::Spec is another