tags:

views:

188

answers:

4

Please explain the Perl code below:

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

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.

Kinopiko
Thanks for the solution.But i need some confirmation here:1- what does this syntax mean : my $class = shift; 2- I have seen in the file 1 more function is being used bith new.Find the full code below. Please explain the other function also and what does it mean by $self variable .Sub poll{My ($self)=@_;$self->{CurrentFile}=”data.xml”;Return $self->http->get(“URL given”);}
@ruchi: you really should buy an introductory Perl book. "Learning Perl" from O'Reilly is excellent.
Ether
Save your money, you can find it all on the web anyway.
Kinopiko
@ruchi123: `perldoc -f shift`
dolmen
+1  A: 

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.

Nikhil Jain
Thanks for the solution. But i need some confirmation here: 1- what does this syntax mean : my $class = shift; 2- I have seen in the file 1 more function is being used bith new. Find the full code below. Please explain the other function also and what does it mean by $self variable . Sub poll { My ($self)=@_; $self->{CurrentFile}=”data.xml”; Return $self->http->get(“URL given”); }
1. my $class = shift, will hold the reference of the package that means let say my $myfoo = Foo->new(); then Foo is package name through which you are calling new() constructor and $myfoo will hold reference of the package which will pass to $class.2. In Perl sub means subroutine i.e.; method or function, so after creating an object you can call subroutines of the class eg. $self->poll();basically $self is hash based object so inside poll function you are making one more key 'CurrenFile' containing data.xml file and then it will return value.
Nikhil Jain
+11  A: 

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.

Chris Johnsen
++ : First-rate explanation!
Zaid
++: Glad to know my answers aren't the only ones that are lengthy and descriptive, this is Stack Overflow done right. All other answers after this should be downvoted for failure to read this.
vol7ron
A: 

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
dolmen