Call parent method indirectly

Discussion in 'Perl Misc' started by Eric J. Roode, Jul 3, 2003.

  1. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    To call a method in a class indirectly, you do:

    $self->$method(@args);

    To call a specific method in the parent class, you do:

    $self->SUPER::foo(@args);

    How do you delegate to a parent method indirectly? This:

    $self->SUPER::$method(@args);

    doesn't work -- it reports "Bad name after ::".

    Is there a way to do this without eval? I am using AUTOLOAD to generate
    simple accessor/mutator methods in my class (table-driven design), and want
    to delegate unknown methods to the parent class (since many methods will be
    inherited from the parent).

    - --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPwQA8GPeouIeTNHoEQIBIQCcCLPo8nnh8y9liZ5t/XomMzkTk90An2Bj
    kfso0B0KoynkEUulLEh3JTKr
    =CGuJ
    -----END PGP SIGNATURE-----
    Eric J. Roode, Jul 3, 2003
    #1
    1. Advertising

  2. Eric J. Roode

    Malte Ubl Guest

    Eric J. Roode wrote:
    > How do you delegate to a parent method indirectly? This:
    >
    > $self->SUPER::$method(@args);


    my $super = "SUPER::$method";
    $self->$super(@args)

    > doesn't work -- it reports "Bad name after ::".
    >
    > Is there a way to do this without eval? I am using AUTOLOAD to generate
    > simple accessor/mutator methods in my class (table-driven design), and want
    > to delegate unknown methods to the parent class (since many methods will be
    > inherited from the parent).


    Maybe you want to use delegation to delegate (as opposed to inheritance)?

    bye
    malte
    Malte Ubl, Jul 3, 2003
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Malte Ubl <> wrote in news:be16hb$32n$:

    > Eric J. Roode wrote:
    >> How do you delegate to a parent method indirectly? This:
    >>
    >> $self->SUPER::$method(@args);

    >
    > my $super = "SUPER::$method";
    > $self->$super(@args)


    Interesting. I will try it.


    >> Is there a way to do this without eval? I am using AUTOLOAD to
    >> generate simple accessor/mutator methods in my class (table-driven
    >> design), and want to delegate unknown methods to the parent class
    >> (since many methods will be inherited from the parent).

    >
    > Maybe you want to use delegation to delegate (as opposed to
    > inheritance)?


    I'm sorry, I was using the word "delegate" loosely. :)

    - --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPwRbz2PeouIeTNHoEQJ9GQCeMjozWvXlPlDGEtwqKZ3rGuaLqjkAoOLZ
    BbrFtXFYxHMIEt7ZMl1eFo1S
    =64Vv
    -----END PGP SIGNATURE-----
    Eric J. Roode, Jul 3, 2003
    #3
  4. Eric J. Roode

    Bart Lateur Guest

    Steve Grazzini wrote:

    >You can also do:
    >
    > $self->can("SUPER::$method")->(@args);


    You forgot to pass along the object itself.

    $self->can("SUPER::$method")->($self, @args);


    I was wondering if there wasn't another way, something like

    $self->SUPER->can($method)

    but I guess not... :)

    --
    Bart.
    Bart Lateur, Jul 4, 2003
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Bart Lateur <> wrote in
    news::

    > Steve Grazzini wrote:
    >
    >>You can also do:
    >>
    >> $self->can("SUPER::$method")->(@args);

    >
    > You forgot to pass along the object itself.
    >
    > $self->can("SUPER::$method")->($self, @args);


    I had no idea that can() returned a function pointer. I thought it was
    just a boolean (1 or '').

    I should know by now that when Perl has the option of returning something
    *useful* as a true value, it does! Thanks, Steve and Bart.

    - --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPwVq5WPeouIeTNHoEQK8owCg2QWpwfR5qSDTwNCt8FlM0TDSjfAAoI3B
    Vv7y8R1WvlGgqC01osoFHij8
    =BUwY
    -----END PGP SIGNATURE-----
    Eric J. Roode, Jul 4, 2003
    #5
  6. Bart Lateur <> wrote:
    > Steve Grazzini wrote:
    > >
    > > $self->can("SUPER::$method")->(@args);

    >
    > You forgot to pass along the object itself.
    >
    > $self->can("SUPER::$method")->($self, @args);
    >
    > I was wondering if there wasn't another way, something like
    >
    > $self->SUPER->can($method)
    >
    > but I guess not... :)


    Yeah -- the first silly thing I tried was

    $self->SUPER::can($method)->

    :)

    --
    Steve
    Steve Grazzini, Jul 5, 2003
    #6
  7. Malte Ubl wrote:
    >
    > Bart Lateur wrote:
    >
    > > Steve Grazzini wrote:
    > >
    > >
    > >>You can also do:
    > >>
    > >> $self->can("SUPER::$method")->(@args);

    > >
    > >
    > > You forgot to pass along the object itself.
    > >
    > > $self->can("SUPER::$method")->($self, @args);

    >
    > Not that many people would care, but this is not thread safe!


    It's only thread-unsafe if you're using the very deprecated 5.005
    threading model.

    > The return value of C<can> can change over time, and time passes
    > between: $self->can("SUPER::$method")
    > and:
    > ->($self, @args);


    Maybe, but AFAIK, the only way that could possibly occur between the two
    operations, in either non-threaded perl, or in perl with ithreads, would
    be if we altered something through a signal handler.

    Futhermore, so what? If @ISA of a module changes, or some method gets
    added... well, we end up calling what can() returned, which is
    "outdated" by a few microseconds, instead of the new value. If we'd
    called the method directly, instead of with the intervention of can(),
    then we'd probably still have gotten the "wrong" subroutine.

    A slightly bigger deal is that ->can() might not call UNIVERSAL::can,
    but instead call some class's overriden version of it.

    > No biggie, if you are not messing with @ISA at runtime or put code
    > references in it:
    >
    > push @ISA, sub { rand > 0.5 ? "Hello::World" : "Destroy::HD" }


    I know that you can put coderefs into @ISA, but I've never heard of them
    going into @INC.

    --
    $a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
    );{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
    Benjamin Goldberg, Jul 8, 2003
    #7
  8. Eric J. Roode

    Bart Lateur Guest

    Benjamin Goldberg wrote:

    >> No biggie, if you are not messing with @ISA at runtime or put code
    >> references in it:
    >>
    >> push @ISA, sub { rand > 0.5 ? "Hello::World" : "Destroy::HD" }

    >
    >I know that you can put coderefs into @ISA, but I've never heard of them
    >going into @INC.


    Heh. For me it's the other way around. For you too, I suppose.

    --
    Bart.
    Bart Lateur, Jul 8, 2003
    #8
    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. Wells
    Replies:
    5
    Views:
    3,258
    Thomas Weidenfeller
    May 17, 2004
  2. python newbie
    Replies:
    4
    Views:
    332
    python newbie
    Nov 30, 2003
  3. swisscheese

    Passing a method indirectly

    swisscheese, Mar 4, 2006, in forum: Python
    Replies:
    4
    Views:
    258
    swisscheese
    Mar 5, 2006
  4. Kavya

    Accessing structure members indirectly

    Kavya, Oct 27, 2006, in forum: C Programming
    Replies:
    14
    Views:
    524
  5. Replies:
    2
    Views:
    1,045
    Marek Vondrak
    Feb 11, 2006
Loading...

Share This Page