Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

perl oop concept

by mendeepak (Scribe)
on Dec 04, 2012 at 06:55 UTC ( [id://1007018] : perlquestion . print w/replies, xml ) Need Help??

mendeepak has asked for the wisdom of the Perl Monks concerning the following question:

package a; sub new { my $class = shift; my $self = {}; bless $self,$class; return $self; } my $obj = new a (); $obj ->{$self}{avalue}=10; print $obj->{$self}{avalue};

please go through this code and will $self will get the avalue as its key at the end , i mean will it become $self={ avalue = value};


Replies are listed 'Best First'.
Re: perl oop concept
by tobyink (Canon) on Dec 04, 2012 at 07:07 UTC

    This looks like OOP code:

    package a; sub new { my $class = shift; my $self = {}; bless $self,$class; return $self; } my $obj = new a ();

    However, the last line there is generally considered poor style. Try:

    my $obj = "a"->new();

    But these lines seem to bear no relation to OOP:

    $obj ->{$self}{avalue}=10; print $obj->{$self}{avalue};

    Try something like this:

    { package a; sub new { my $class = shift; my $self = {}; bless $self,$class; return $self; } sub avalue { my $self = shift; $self->{avalue} = shift if @_; return $self->{avalue}; } } my $obj = "a"->new(); $obj->avalue(10); # set print $obj->avalue, "\n"; # get

    That should give you an idea how OOP works. That said, I'd recommend using Moose or Moo as a way of reducing repetitive OOP code...

    { package a; use Moo; has avalue => (is => 'rw'); } my $obj = "a"->new(); $obj->avalue(10); # set print $obj->avalue, "\n"; # get
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: perl oop concept
by dsheroh (Monsignor) on Dec 04, 2012 at 10:16 UTC
    Try it and see.

    As written, this won't compile under strict because $self doesn't exist outside of new, so I've removed that. The $self from within new is returned as its output and assigned to $obj, so I think where you wrote $obj->{$self}, you really just meant $obj.

    With that minor adjustment and adding a call to Data::Dumper to show what's in $obj, the code becomes

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; package a; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } package main; my $obj = a->new; $obj->{avalue} = 10; print Dumper($obj);
    and produces the output
    $VAR1 = bless( { 'avalue' => 10 }, 'a' );
    Of course, as already mentioned, it would be much better OO practice to create an accessor for avalue instead of working directly with the object's internals, but at least you now know a way to see whether your objects contain the data you expect them to.

      Thanks dsheroh. Got how the Script works.

Re: perl oop concept
by DrHyde (Prior) on Dec 04, 2012 at 11:14 UTC

    I don't see the string 'value' anywhere in your code (apart from as a substring of 'avalue'), and no operations on 'avalue' to strip off the leading 'a'. So the answer is no.

    This is not the answer that you wanted though. There are many articles in the monastery about how to ask good questions. I suggest you read some of them.

Re: perl oop concept
by sundialsvc4 (Abbot) on Dec 04, 2012 at 16:41 UTC

    By all means, don’t make this a voyage of discovery.   Read the various perldocs on OOP in Perl, and seek out a few recent Perl modules at   Read the source code of some of them.   (Or just look at the modules already installed on your own system.)   Copy their practices exactly.

    Remember the essence of what bless does:   it associates a package-name with some data-object.   It thereby enables the $foo->subname vernacular to be used.   Perl knows to look in the associated package to find the routine, and it calls that routine with $foo as the first parameter.   That’ s basically it.   The data object didn’t cease to be what it is ... the rules for accessing it as a data object haven’t really changed ... it’s merely wearing a halo now, and glowing slightly (and wearing a beneficent smile ...).

    (Am I simplifying?   Of course.   But not really over-simplifying, all in the name of simple clarity.)