programming in perl references and objects

22
Programming in Perl references and objects Peter Verhás January 2002.

Upload: evadne

Post on 06-Jan-2016

26 views

Category:

Documents


2 download

DESCRIPTION

Programming in Perl references 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; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming in Perl references and objects

Programming in Perlreferences and objects

Peter VerhásJanuary 2002.

Page 2: Programming in Perl references and objects

References (pointers)

$apple = 113;

$pear = 'apple';

$plum = \$apple;

print "$$pear = $$plum\n";

OUTPUT:113 = 113 $pear is soft reference

$plum is hard reference

Page 3: Programming in Perl references and objects

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.

Page 4: Programming in Perl references and objects

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

Page 5: Programming in Perl references and objects

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} }

OUTPUT:113 = 113

Variables can reference arrays, hash variables, functions and

objects (see later).

Page 6: Programming in Perl references and objects

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 [

Page 7: Programming in Perl references and objects

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 {

Page 8: Programming in Perl references and objects

Symbol tables

fooSCALARARRAYHASHCODEIOGLOB

$foo@foo%foosub foo { 1; }open(foo,”file.txt”)

*foo{SCALAR}*foo{ARRAY}*foo{HASH}*foo{CODE}*foo{IO}*foo{GLOB}

$main::

Page 9: Programming in Perl references and objects

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:1202peach1@foo = (1,2,3,4,5,6,);*main::foo

Page 10: Programming in Perl references and objects

Practical use of typeglob

*a = *b;– a becomes the same as b

*PI = \3.1415926;– Defines unalterable symbol

Page 11: Programming in Perl references and objects

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

Page 12: Programming in Perl references and objects

Writing your own module

{

package fruit;

sub worm {

$apricot = 'apple';

'carotene' }

}

$apricot = &fruit::worm;

print "$apricot, $fruit::apricot\n";

Page 13: Programming in Perl references and objects

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

Page 14: Programming in Perl references and objects

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; }

OUTPUT:13865742

package main; is the default

Page 15: Programming in Perl references and objects

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

Page 16: Programming in Perl references and objects

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).

Page 17: Programming in Perl references and objects

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.

Page 18: Programming in Perl references and objects

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.

Page 19: Programming in Perl references and objects

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!

Page 20: Programming in Perl references and objects

Class Example

{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"; }

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);

baseclass f0 1myclass f1 1myclass f2 1autoload myclass myclass::f3autoload myclass myclass::DESTROY

OUTPUT:

Page 21: Programming in Perl references and objects

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.

Page 22: Programming in Perl references and objects

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.