What does this Perl code do?

Please explain the Perl code below:

sub new {
    my $class = shift;
    my $self = {property => 'u', provider => 'ramesh'};
    bless $self, $class;
    return $self;
}

Answers


It is a constructor for a Perl object. Perl objects can be made from any Perl reference. This one happens to be made from a hash reference (the {property => …} code creates a hash reference). See the perlobj manpage.

Usually you would find such code in a package, which is the container for the methods of a class and its instances:

package Foo;

sub new {
    my $class = shift;                                   # 2
    my $self = {property => 'u', provider => 'ramesh'};  # 3
    bless $self, $class;                                 # 4
    return $self;                                        # 5
}

sub poll {
    my ($self)=@_;                                       # 7
    $self->{CurrentFile}="data.xml";                     # 8
    return $self->http->get("URL given");                # 9
}

Then in some other code you use it like this:

my $myfoo = Foo->new();                                  # 1

Now $myfoo holds a instance of Foo. You can then invoked the object’s methods:

$myfoo->poll();                                          # 6

Line-by-line description (replace Foo with whatever the actual package name is in the code):

  1. my $myfoo = Foo->new(); This calls the the “class method” new of the class Foo and stores the result in the scalar variable $myfoo. This call is technically the same as Foo::new("Foo") (without having to repeat the Foo). See Method Invocation in perlobj.

  2. my $class = shift; The argument list provided in the call to new contains only the class name. shift removes and returns the first value from the @_ array variable that holds the parameter values given when the method is called. See the perlsub for details on parameter passing and the @_ array. So, this statement stores the class name "Foo" in a local scalar variable $class.

  3. my $self = {property => 'u', provider => 'ramesh'}; Create a reference to a new, anonymous hash {…} with some initial key/value pairs and store it in the local scalar variable $self. See perlref for details on creating references.

  4. bless $self, $class; Turn the hash reference stored in $self into a Foo object, by “blessing” it into the Foo class (remember, $class eq "Foo").

  5. return $self; Return the newly created object.

  6. $myfoo->poll(); Call the poll method of the object held in the scalar variable $myfoo.

  7. my ($self)=@_; The first parameter to instance methods is the object instance itself. This statement stores the first value from the @_ parameter array variable into a new local scalar variable $self.

  8. $self->{CurrentFile}="data.xml"; We know from new that $self will be a hash reference. This statement assigns a value to the CurrentFile key in the hash reference that embodies the object.

  9. return $self->http->get("URL given"); Call the http method on the $self object. On the object return returned from the http method, call the get method (passing "URL given" as an argument). Return the result of the call to get.


This code on its own doesn't do anything. But, if you have a file

package Why::Am::I::So::Funky;

sub new {
    my $class = shift;
    my $self = {property => 'u', provider => 'ramesh'};
    bless $self, $class;
    return $self;
}

1;

and another file like so

use Why::Am::I::So::Funky;
my $funky = Why::Am::I::So::Funky->new ();

then that would create an object of the Why::Am::I::So::Funky class called $funky.


In this code you are constructing an object of class. here $self is a hash containing two key entries,property and provider and their respective values.

bless is inbuilt function of Perl that takes either one or two parameters,the first argument is a referent and the second is the package to bless the referent into.if the second argument is omitted then the current package is used.

Now after blessing you can used $self->{property} that will give you 'u' and $self->{provider} that will give you 'ramesh', in the current package.

Best way to learn Perl is to read standard book Programming Perl by the inventor itself.


Read perlboot, the Beginner's Object-Oriented Tutorial, and perltoot, Tom's object-oriented tutorial for Perl, to learn object-oriented programming in Perl:

perldoc perlboot
perldoc perltoot

Need Your Help

Running two threads at the same time

multithreading parallel-processing

I want to know if a program can run two threads at the same time (that is basically what it is used for correct?). But if I were to do a system call in one function where it runs on thread A, and h...

Extension of constructed generic type in Swift

generics swift

Is it possible to extend an generic class for a specialised/constructed generic type? I would like to extend Int Arrays with a method to calculate the sum of its elements.