Programming in Perl references and objects Peter Verhs
Programming in Perl references and objects Peter Verhás January 2002.
References (pointers) $apple = 113; $pear = 'apple'; $plum = $apple; print "$$pear = $$plumn"; OUTPUT: 113 = 113 $pear is soft reference $plum is hard reference
Easy memory handling $apple = 114; sub pointir { my $apple = 113; return $apple; } There is nothing like malloc() in C. $pear = 'apple'; $plum = &pointir; Do not try to allocate print "$$pear = $$plumn"; memory, Perl will take care. Don’t try to release memory, OUTPUT: Perl will take care. 114 = 113 Pay attention to programming instead.
Only space is reserved not variable sub pointir { my $apple = 113; The local variable return $apple; $apple is not kept, only } the memory where the $pear = &pointir; value is stored. $plum = &pointir; $$plum++; $$plum and print "$$pear < $$plumn"; $$pear are two different variables OUTPUT: 113 < 114
Complex Reference Variable Example $apple = sub { my @apple = (1, 1, 3); return @apple; }; $melon = sub { my %apple = (0, 1, 1, 1, 2, 3); return %apple; }; $pear = &$apple; $plum = &$melon; $$pear[2]++; $pear->[2]--; $$plum{2}++; $plum->{2}--; for $i (0. . 2){ print $$pear[$i] } print ' = '; for $i (0. . 2){ print $$plum{$i} } Variables can reference arrays, hash variables, functions and objects (see later). OUTPUT: 113 = 113
Array of arrays (1) @Ao. A = ( [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ); $ref_to_Ao. A = [ [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ]; print $Ao. A[2][2]; print $ref_to_Ao. A->[2][2]; print $Ao. A[2]->[2]; print $ref_to_Ao. A->[2]; You can omit -> from between ] and [
Array of arrays (2) @Ao. A = ( { "fred„ => "barney" }, { "george", "jane" }, [ "homer", "marge", "bart" ], ); print $Ao. A[2][2]; print $Ao. A[0]{’fred’}; You can omit -> from • between ] and [ • between ] and { • between } and [ • between } and {
Symbol tables $main: : foo SCALAR ARRAY HASH CODE IO GLOB *foo{SCALAR} *foo{ARRAY} *foo{HASH} *foo{CODE} *foo{IO} *foo{GLOB} $foo @foo %foo sub foo { 1; } open(foo, ”file. txt”)
Typeglob @foo = (1, 2, 3, 4, 5, 6, ); $foo = 120; %foo = ( 1=>2, 2=>3, 'apple' =>'peach'); sub foo { 1; } open(fo, "test. pl") or die; print ${*foo{SCALAR}}; print "n"; print ${*foo{ARRAY}}[1]; print "n"; print ${*foo{HASH}}{'apple'}; print "n"; print &{*foo{CODE}}; print "n"; $fh = *fo{IO}; print scalar <$fh>; print ${*foo{GLOB}}; print "n"; OUTPUT: 120 2 peach 1 @foo = (1, 2, 3, 4, 5, 6, ); *main: : foo
Practical use of typeglob *a = *b; – a becomes the same as b *PI = 3. 1415926; – Defines unalterable symbol
Modules • Modules written by others – use CGI; and use it, don’t care how it is written (CGI is an example, there a lot of modules) • Write your own modules
Writing your own module { package fruit; sub worm { $apricot = 'apple'; 'carotene' } } $apricot = &fruit: : worm; print "$apricot, $fruit: : apricotn";
Special subroutines in the module • BEGIN – A BEGIN subroutine is executed as soon as possible. Unloaded after execution. • CHECK – After compilation, before runtime reverse order • INIT – After compilation, before runtime forward order • END – An END subroutine is executed as late as possible
Special subroutines example BEGIN { print 1; } END {print 2; } BEGIN { print 3; } END { print 4; } INIT { print 5; } CHECK {print 6; } INIT { print 7; } CHECK {print 8; } package main; is the default OUTPUT: 13865742
Putting a module into a separate file package modul 1; BEGIN { print "modul 1 beginn"; } END { print "modul 1 endn"; } sub hello { print „I love you!n"; } 1; push @INC, ’. ’; use module 1; &modul 1: : hello; @INC holds all directories where the modules are looked for. BEGIN and END are called when the module is loaded and unloaded. TRUE value signaling successful load
Objects in Perl • There is no „class” in Perl • Object is a reference to something „blessed” into a class • package myclass; • sub new { bless {}; } Any reference can be blessed into any module (or in this case rather call it class).
Creating a new object • Any function can create a new reference and bless it into a class. • The creator should allocate the memory even though this is not a big deal in Perl. – bless {} actually creates the reference and blesses into actual class.
Class is a package • Class Method is a sub in the module • Inheritance through the package array @ISA sub method { my $class = shift; . . } The first argument passed to a sub is the object.
Ways Calling Methods • $object->method(args) • method $object args – $object = new Class initargs; – print STDERR ”I warn you!” object->method need parentheses if there is argument. Always use -> notation!
Class Example { package baseclass; sub f 0 { my $class = shift; my $arg = shift; print "baseclass f 0 $argn"; } sub f 1 { my $class = shift; my $arg = shift; print "baseclass f 1 $argn"; } } { package myclass; push @ISA, 'baseclass'; sub new { bless {}; } sub f 1 { my $class = shift; my $arg = shift; print "myclass f 1 $argn"; } sub f 2 { my $class = shift; my $arg = shift; print "myclass f 2 $argn"; } sub AUTOLOAD { my $class = shift; print "autoload myclass $AUTOLOADn"; } } $q = new myclass; $q->f 0(1); f 1 $q 1, 2, 3 ; myclass: : ->f 2(1); $q->f 3(1); OUTPUT: baseclass f 0 1 myclass f 1 1 myclass f 2 1 autoload myclass: : f 3 autoload myclass: : DESTROY
Method DESTROY • If this function is defined is automatically called when the object is not used any more. • It gets only one argument, the object. • This argument is read-only, but can be blessed into other class so that DESTROY for that class will be called.
Thank you for your kind attention and I hope that you learnt a lot from this tutorial and will help you learning Perl giving you a jump start.
- Slides: 22