220 likes | 318 Views
Programming in Perl references and objects. Peter Verhás January 2002. References (pointers). $apple = 113; $pear = 'apple'; $plum = $apple; print "$$pear = $$plum<br>"; OUTPUT: 113 = 113. $pear is soft reference $plum is hard reference. Easy memory handling. $apple = 114;
E N D
Programming in Perlreferences and objects Peter Verhás January 2002.
References (pointers) $apple = 113; $pear = 'apple'; $plum = \$apple; print "$$pear = $$plum\n"; OUTPUT: 113 = 113 $pear is soft reference $plum is hard reference
Easy memory handling $apple = 114; sub pointir { my $apple = 113; return \$apple; } $pear = 'apple'; $plum = &pointir; print "$$pear = $$plum\n"; OUTPUT: 114 = 113 There is nothing like malloc() in C. Do not try to allocate memory, Perl will take care. Don’t try to release memory, Perl will take care. Pay attention to programming instead.
Only space is reserved not variable sub pointir { my $apple = 113; return \$apple; } $pear = &pointir; $plum = &pointir; $$plum++; print "$$pear < $$plum\n"; OUTPUT: 113 < 114 The local variable $apple is not kept, only the memory where the value is stored. $$plum and $$pear are two different variables
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) @AoA = ( [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ); $ref_to_AoA = [ [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ]; print $AoA[2][2]; print $ref_to_AoA->[2][2]; print $AoA[2]->[2]; print $ref_to_AoA->[2]->[2]; You can omit->from between] and [
Array of arrays (2) @AoA = ( { "fred„ => "barney" }, { "george", "jane" }, [ "homer", "marge", "bart" ], ); print $AoA[2][2]; print $AoA[0]{’fred’}; You can omit -> from • between ] and [ • between ] and { • between } and [ • between } and {
Symbol tables *foo{SCALAR} *foo{ARRAY} *foo{HASH} *foo{CODE} *foo{IO} *foo{GLOB} $main:: foo SCALAR ARRAY HASH CODE IO 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 are 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::apricot\n";
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 modul1; BEGIN { print "modul1 begin\n"; } END { print "modul1 end\n"; } sub hello { print „I love you!\n"; } 1; push @INC, ’.’; use module1; &modul1::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 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 sub f2 { my $class = shift; my $arg = shift; print "myclass f2 $arg\n"; } sub AUTOLOAD { my $class = shift; print "autoload myclass $AUTOLOAD\n"; } } $q = new myclass; $q->f0(1); f1 $q 1,2,3 ; myclass::->f2(1); $q->f3(1); { package baseclass; sub f0 { my $class = shift; my $arg = shift; print "baseclass f0 $arg\n"; } sub f1 { my $class = shift; my $arg = shift; print "baseclass f1 $arg\n"; } } { package myclass; push @ISA, 'baseclass'; sub new { bless {}; } sub f1 { my $class = shift; my $arg = shift; print "myclass f1 $arg\n"; } OUTPUT: baseclass f0 1 myclass f1 1 myclass f2 1 autoload myclass myclass::f3 autoload myclass myclass::DESTROY
Method DESTROY • If this function is defined is automatically called whenthe 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.