·  PERL – Syntax - Other – Classes

 

- Info:

  - PERL is not OOP-Object Oriented Programming language.

    You have to manualy organize your code by using bless function and anonymous hashes to make it feel like OOP.

  - In Perl, any package can be used as a "class".

  - Any function in a package becomes class/object method.

  - By far the most common mechanism used in Perl to represent a class is an anonymous hash.

    That's because a hash has an arbitrary number of data fields, each conveniently accessed by an arbitrary name.

  - Additional info can be found at:

    http://www.perl.com/doc/manual/html/pod/perltoot.html   

    http://www.codeproject.com/perl/camel_poop.asp

 

- Bless():

  - Function bless simply marks variable $self, which was given as argument, as "belonging to current package" Person.

    PERL needs this to find functions when they are called by using such blessed variable when package is not specified.

  - Normal way to call a function name, which is in another package Person, would be:

    Person->name ("Jason");    #Package name Person is inserted as first argument to function call.

  - If instead of package name Person we use our blessed reference $him IT will be inserted as first argument:

    $him  ->name ("Jason");    #Reference $him      is inserted as first argument to function call.

    PERL knows that function name is in package Person because reference $him is flaged as belonging to that package.

  - Second argument to function bless can be string containing name of the class/package making constructor inheritable.

 

    Person.pl

    #PACKAGE PERSON.--------------------------------------

    package Person;

    

    #Constructor.

    sub new {    

        my $self = {};          #Create reference to anonymous hashtable.

           $self->{NAME}  = undef;

           $self->{AGE}   = undef;

           $self->{PEERS} = [];

        bless ($self);              #Reference $self gets flaged as object of class/package Person.

        return $self;

    }

    

    #Methods to access object data.

    #With    args, they set the value.

    #Without args, they only retrieve it/them.       

    

    sub name {

        my $self = shift;            #Get package name automaticly inserted as first argument.

        if (@_) { $self->{NAME} = shift }  #Check if there are more arguments.

        return    $self->{NAME};

    }

    

    sub age {

        my $self = shift;

        if (@_) { $self->{AGE} = shift }

        return    $self->{AGE};

    }

    

    sub peers {

        my $self = shift;

        if (@_) { @{ $self->{PEERS} } = @_ }

        return    @{ $self->{PEERS} };

    }

    

    #PACKAGE MAIN.--------------------------------------

    package main;

    

    $him = Person->new();

    $him->name ("Jason");        #First input argument is this object itself.

    $him->age  (23);

    $him->peers("Norbert", "Rhys", "Phineas");

    

    printf "%s is %d years old.\n", $him->name, $him->age;

    print  "His peers are: "      , join(", ", $him->peers), "\n";