Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Call parent method indirectly

Reply
Thread Tools

Call parent method indirectly

 
 
Eric J. Roode
Guest
Posts: n/a
 
      07-03-2003
-----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-----
 
Reply With Quote
 
 
 
 
Malte Ubl
Guest
Posts: n/a
 
      07-03-2003
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

 
Reply With Quote
 
 
 
 
Eric J. Roode
Guest
Posts: n/a
 
      07-03-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Malte Ubl <(E-Mail Removed)> wrote in news:be16hb$32n$(E-Mail Removed):

> 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/AwUBPwRbz2PeouIeTNHoEQJ9GQCeMjozWvXlPlDGEtwqKZ3rGu aLqjkAoOLZ
BbrFtXFYxHMIEt7ZMl1eFo1S
=64Vv
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      07-04-2003
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.
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      07-04-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bart Lateur <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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/AwUBPwVq5WPeouIeTNHoEQK8owCg2QWpwfR5qSDTwNCt8FlM0T DSjfAAoI3B
Vv7y8R1WvlGgqC01osoFHij8
=BUwY
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Steve Grazzini
Guest
Posts: n/a
 
      07-04-2003
Bart Lateur <(E-Mail Removed)> 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
 
Reply With Quote
 
Benjamin Goldberg
Guest
Posts: n/a
 
      07-08-2003
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;}
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      07-08-2003
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Accessing structure members indirectly Kavya C Programming 14 10-28-2006 08:35 PM
Passing a method indirectly swisscheese Python 4 03-05-2006 11:16 AM
Reading ENV vars in Solaris - Indirectly - help! Wells Java 5 05-17-2004 11:23 AM
referencing an object attribute sort of indirectly from within list python newbie Python 4 11-30-2003 12:34 PM



Advertisments