October 22 2007
PerlFrom Ground level and Up
Lecture 1
By Shmuel Fomberg
2
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Data Types
Scalar Array Hash IO CODE GLOB
3
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Data Types
Scalar Number (integer, floating) String Number and String Reference to anything. (except IO) undef (actually, a null reference) False – undef, empty string, zero or string that
evaluate to zero
4
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Data Types
Array Contain only scalars Can be undef (obsolete) Has length
scalar(@array) $#array
Can ask if exists $array[2] Can get slices @a2 = @array[3,5] False – only empty/undefined array.
5
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Data Types
Hash Keys – only strings Values – scalars Can ask if exists $hash{key} Can delete $hash{key} Ops: keys, values, each Can be undef (obsolete) Can get slices @a2 = @hash{“key1”, “key2”} False – only empty/undefined hash.
6
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Data Types
Code All the functions in the script are actually Code
variables Can have anonymous functions
my $code = sub {….}
Can ask if exists/defined &function Can run the function
7
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Data Types
IO files handlers, sockets open FH, “<“, “filename”; Global Can not pass to functions Can not use in recursion open $fh, “<“, “filename”;
What’s that?
8
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Data Types
GLOB A “hash” Contains only one scalar, array, hash, code, IO Globals are stored using GLOBs open $fh, “<“, “filename”;
Now $fh contain a reference to anonymous glob
9
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Packages
Main package is called “main”. or “::”
Declaring the name of the current package:package MyPkg;package MyPkg::Private;
Declaring a package-global with “our”our $id;
Accessing the global from elsewhere:$MyPkg::id = 5;
10
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Packages
main
MyPkg
Private
MyPkg
Private
id
GLOB
SCALAR 5
11
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Packages
More on Package-Globals All the Functions Not searched by default (for variables)
More on GLOBs A way to declare constant
*C = \5
A way to push a function *func = sub {….}
12
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Lexicals
When declaring variable with “my” my $x = 5;
Valid only in the current block When outside of any block – to the current file
Can not be accessed from “outside” Possible, but very difficult
Does not use globs Way faster
13
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Closure
Every piece of code remembers where it was defined
my $offset = 5;
sub get_acc {
my $acc = 1;
return sub { return $offset + $acc++; };
}
14
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Local Globals
Temporarily change the value of global (our) variable
Effective until the end of the block. After that, the original value is restoredopen my $fh, "foo" or die $!;local $/; # enable localized slurp modemy $content = <$fh>;close $fh;
{ local *blaY= sub {return 1;}; print defined(&blaY) ? "" : "Not ", "Exists\n";}print defined(&blaY) ? "" : "Not ", "Exists\n";