UTOSC 2008/Perl Fundamentals
SelfGOL an entry in Obfuscated Perl Contest
Perl looks like "Line Noise"
It is really really easy to write bad code.
Compared to bash, Perl is very similar with a few changes. (ie. echo is now print)
When bash gets any logic, it is time to move on. Perl is an option. So is Python and command line PHP.
Very high level language (VHLL)
Perl was originally designed for reporting.
Comments with #. Exception: #!/usr/bin/perl
"sha-bang" #!
Hello world:
#!/usr/bin/perl print "Hello World\n"
Or
perl -e "print 'Hello World!'\n"
Execute single command (for testing purposes):
perl -e [command] perl -e "use strict;" perl -e "use DBD::pg;"
Code blocks. Lines should end with ';'
Bare words. Generally no longer allowed:
print hello world;
Do in strict syntax. Don't allow bare variables. Require variables to be declared:
use strict;
Send to standard error (useful for logging):
# includes line number warn "message"; # no line number warn "message\n";
Exit codes:
exit # return 0 exit 42 # return 42
Die (combination of warn and exit 255):
die "this is dead" die
Variables:
- Scalars - standard variable
- Arrays - indexed list
- Hashes - named indexed
Scalars
$a = 'this is a test';
To convert a string number to a number multiply by 1:
$a = '5'; $a = $a * 1;
Sigils determining data content. A sigil is an indicator as to the context of the variable that you are working with:
- $ scalar
- @ array
- % hashes
String concatenation:
$a = "hello" . "world";
PHP is based off of Perl
- dot '.' string concatination
- dollar sign variables
- die command
Single vs double quotes. Double quotes evaluate variables and escape characters. Single quote treats as exact. (except single quote can be ie 'bob\'s name')
Traditional operators:
- +, -, *, /
Space saving operators:
- +=, -=, *=, /=
- ++, --
Arrays (zero based):
@a = (0, 1, "two", 3, 4, 5); # create array print @a[1..4]; # slice 1 - 4 print $a[3]; # scalar 3 $a[4] = "four"; # arrays are mutable; push $a, "six", 7, "eight"; # append to array @b = (9, 10); # create second array @c = (@a, @b); # create array of array push @a, @b; # append @b to @a use Data::Dumper; print Dumper( @a ); # print @a in formatted list
Foreach:
foreach $a ( @a ) { print $a . "\n"; } # $_ (default variable) is last variable used foreach ( @a ) { print $_ . "\n"; } # for and foreach are interchangable print "$_\n" for @a; # compressed foreach
Join and Split:
print join(', ', @a); # join turns a list into a scalar $c = join(':', @a); # split turns a scalar into a list @d = split(/:/, $c); # opposite of join
Hashes (associative array):
%a = ( 'father', 'anakin', 'son', 'luke', ); # can leave comma in last list; %a = ( 'father' => 'anakin', 'son' => 'luke', ); # can leave comma in last list; print $a{'father'}; print "$a{$_}" for keys %a;
List environment variables:
# todo
Perl can handle decimals. Bash can only do integers.
Conditionals:
%jedi = ( 'father' => 'anakin', 'son' => 'luke', ); if ( $jedi{father} eq 'anakin' ) { ... } elseif (...) { ... } else { ... }
Comparison:
- eq for strings
- == for numbers
Numbers:
$a = 1; $b = 2; if ($a = $b) { ... } # BAD CODE! force equal if ($a == $b) { ... } # ok code
For loop counter:
#todo