helloworld1234
helloworld1234

Reputation: 327

Basic OOP concept in Perl

I'm learning the OOP concept in Perl where the syntax is different from Java, which I used to learn the OOP concept.

I have an example to declare a class Person, but I'm a bit confused.

The code is as follows

package Person;

sub new {

    my $class = shift;

    my $self  = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };

    # Print all the values just for clarification.

    print "First Name is $self->{_firstName}\n";
    print "Last Name is $self->{_lastName}\n";
    print "SSN is $self->{_ssn}\n";

    bless $self, $class;

    return $self;
}

From the example above, is my $self a scalar variable or a hash variable?

As far as I know, hash variables in Perl are declared with % while $ is used for scalar variables.

And what is the use of the bless function? Why does it return $self?

Upvotes: 2

Views: 406

Answers (2)

Dave Cross
Dave Cross

Reputation: 69244

From the example above, is my $self a scalar variable or a hash variable?

$self is a scalar variable. You can tell that because it starts with a $.

(Parenthetical update: In the comments, brian points out that this rule is flawed. And he's right of course - as he usually is. The $ at the start of $self shows that it's a scalar value, not a scalar variable. But if you take the $ at the start together with the lack of look-up brackets - like [...] or {...} - following the variable name, then you can be sure this is a scalar variable.)

But your complete line of code is this:

my $self  = {
    _firstName => shift,
    _lastName  => shift,
    _ssn       => shift,
};

Here, the { ... } is an "anonymous hash constructor". It creates a hash and returns a reference to it. References are always scalar values, so they can be stored in scalar variables (that's one of the major reasons for their existence).

So $self is still a scalar variable, but it contains a reference to a hash.

And what is the use of the bless function?

The call to bless() effectively assign a type to the $self variable. Perl needs to know the type of your object in order to know which symbol table to search for the object's methods. When I'm running classes in this, I like to say that bless() is like writing the type of the object on a post-it note and slapping it on the object's forehead - so, later on, people can know what the type is.

Why does it return $self?

You will call this method something like this:

my $person = Person->new(...); # passing various parameters

The new() method needs to return the newly created object in order that you can store it in a variable and manipulate it in some way later on.

But a Perl subroutine returns the value of the last expression in the subroutine, and bless() returns the "blessed" object, so it would be fine to end the subroutine with the previous line:

bless $self, $class;

But it's traditional to be more explicit about return values, so most people would add the return line. It makes for better documented code.

Upvotes: 1

jcaron
jcaron

Reputation: 17710

$self is a hash reference, so it's considered as a scalar in most manipulations.

bless is used to say that this hash reference is an object of the given class (which will be Person here unless you have child classe), so that you can call functions of this class using the $object->function notation.

And the return $self is necessary so you can actually retrieve the created object when you call new!

Upvotes: 0

Related Questions