perl from the ground up: variables and data types
DESCRIPTION
a basic introduction to variable types in PerlTRANSCRIPT
![Page 1: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/1.jpg)
October 22 2007
PerlFrom Ground level and Up
Lecture 1
By Shmuel Fomberg
![Page 2: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/2.jpg)
2
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Data Types
Scalar Array Hash IO CODE GLOB
![Page 3: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/3.jpg)
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
![Page 4: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/4.jpg)
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.
![Page 5: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/5.jpg)
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.
![Page 6: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/6.jpg)
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
![Page 7: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/7.jpg)
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?
![Page 8: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/8.jpg)
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
![Page 9: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/9.jpg)
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;
![Page 10: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/10.jpg)
10
Perl – From The Ground and UP
October 22 2007Shmuel Fomberg
Packages
main
MyPkg
Private
MyPkg
Private
id
GLOB
SCALAR 5
![Page 11: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/11.jpg)
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 {….}
![Page 12: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/12.jpg)
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
![Page 13: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/13.jpg)
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++; };
}
![Page 14: Perl from the ground up: variables and data types](https://reader035.vdocuments.us/reader035/viewer/2022070320/558791c7d8b42a750b8b45c2/html5/thumbnails/14.jpg)
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";