overridden method in perltoot

Discussion in 'Perl Misc' started by cmic, Aug 13, 2009.

  1. cmic

    cmic Guest

    Hi
    context : PerlOO Newbie.
    I don't understand why this excerpt from perltoot doesn't work as
    expected.
    It should print "In Employe::peers", but prints "In Person::peers"
    instead.
    Moreover, the complete example in perltoot ("Overridden method"
    paragraph) doesn't work either.
    Or do I miss something ?

    --------file Person.pm-------------
    #!/usr/local/bin/perl
    package Person;
    use strict; use warnings;
    sub new {
    my $self = {};
    $self->{NAME} = undef;
    bless($self);
    return $self;
    }
    sub name {}

    sub peers { print "in Person::peers\n";}
    1;

    --------------file Employe.pm -----------
    #!/usr/local/bin/perl
    package Employe;
    use Person;
    use vars qw (@ISA);

    use strict; use warnings;
    @ISA="Person";

    sub peers {
    printf "In Employe::peers\n";
    }

    ----------file a.pl -----------
    #!/usr/local/bin/perl
    use Employe;
    use strict;
    use warnings;

    my $emp=Employe->new();
    print $emp->peers(), "\n";


    TYA
    --
    michel marcon aka cmic
    Unix Sysadmin
     
    cmic, Aug 13, 2009
    #1
    1. Advertising

  2. cmic

    Dave Weaver Guest

    On Thu, 13 Aug 2009 07:34:53 -0700 (PDT), cmic <> wrote:
    > Hi
    > context : PerlOO Newbie.
    > I don't understand why this excerpt from perltoot doesn't work as
    > expected.
    > It should print "In Employe::peers", but prints "In Person::peers"
    > instead.
    > Moreover, the complete example in perltoot ("Overridden method"
    > paragraph) doesn't work either.
    > Or do I miss something ?


    It appears that you missed the perltoot section
    "Planning for the Future: Better Constructors" where the constructor
    is revised.


    > --------file Person.pm-------------
    > #!/usr/local/bin/perl
    > package Person;
    > use strict; use warnings;
    > sub new {
    > my $self = {};
    > $self->{NAME} = undef;
    > bless($self);


    This blesses $self into the current package (i.e. Person) so, even
    when called via the Employee constructor, the result is always a
    Person, not an Employee.

    You need to bless $self into the correct class (which is usually the
    first parameter passed to the constructor):

    sub new {
    my $class = shift;

    # ...

    return bless $self, $class;
    }

    Now, when called via: $x = Person->new()
    $x will be a Person, but when called via: $x = Employee->new()
    then $x will be an Employee.
     
    Dave Weaver, Aug 13, 2009
    #2
    1. Advertising

  3. cmic

    cmic Guest

    Hello

    On 13 août, 16:56, Tad J McClellan <> wrote:
    > cmic <> wrote:
    > > I don't understand why this excerpt from perltoot doesn't work as
    > > expected.

    ....
    >     bless($self, $class);
    >
    > >     return $self;
    > > }

    >
    > See the "Planning for the Future: Better Constructors" section in perltoot,
    > and note what the single-arg form of bless() does:
    >
    >     perldoc -f bless
    >
    >     ... Always use the two-argument version if a derived class might
    >     inherit the function doing the blessing...


    OK. I dig it. (In short, this another form of RTFM :cool: )
    Thanks to Tad and Dave.

    --
    michel marcon aka cmic
     
    cmic, Aug 13, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patel
    Replies:
    6
    Views:
    40,550
    Randall R Schulz
    Oct 23, 2003
  2. Patel
    Replies:
    1
    Views:
    419
    Harald Hein
    Oct 23, 2003
  3. Himanshu Garg
    Replies:
    1
    Views:
    166
    Himanshu Garg
    Sep 21, 2003
  4. perltoot bug?

    , Jul 11, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    106
  5. jh3an
    Replies:
    2
    Views:
    140
Loading...

Share This Page