SUPER(3pm) | User Contributed Perl Documentation | SUPER(3pm) |
SUPER - control superclass method dispatch
Find the parent method that would run if this weren't here:
sub my_method { my $self = shift; my $super = $self->super('my_method'); # Who's your daddy? if ($want_to_deal_with_this) { # ... } else { $super->($self, @_) } }
Or Ruby-style:
sub my_method { my $self = shift; if ($want_to_deal_with_this) { # ... } else { super; } }
Or call the super method manually, with respect to inheritance, and passing different arguments:
sub my_method { my $self = shift; # parent handles args backwardly $self->SUPER( reverse @_ ); }
When subclassing a class, you occasionally want to dispatch control to the superclass -- at least conditionally and temporarily. The Perl syntax for calling your superclass is ugly and unwieldy:
$self->SUPER::method(@_);
especially when compared to its Ruby equivalent:
super;
It's even worse in that the normal Perl redispatch mechanism only dispatches to the parent of the class containing the method at compile time. That doesn't work very well for mixins and roles.
This module provides nicer equivalents, along with the universal method "super" to determine a class' own superclass. This allows you to do things such as:
goto &{$_[0]->super('my_method')};
if you don't like wasting precious stack frames.
If you are using roles or mixins or otherwise pulling in methods from other packages that need to dispatch to their super methods, or if you want to pass different arguments to the super method, use the "SUPER()" method:
$self->SUPER( qw( other arguments here ) );
This module provides the following functions and methods:
This takes no arguments; it passes the same arguments passed to the currently-executing method.
The module exports this function by default.
Note: you must have the appropriate "package" declaration in place for this to work. That is, you must have compiled the method in which you use this function in the package from which you want to use it. Them's the breaks with Perl 5.
The module does not export this function by default. Call it directly.
Beware: if you do weird things with code generation, be sure to name your anonymous subroutines. See Perl Hacks #57.
Using "super" doesn't let you pass alternate arguments to your superclass's method. If you want to pass different arguments, use "SUPER" instead. D'oh.
This module does a small amount of Deep Magic to find the arguments of method calling "super()" itself. This may confuse tools such as "Devel::Cover".
In your own code, if you do complicated things with proxy objects and the like, define "__get_parents()" to return a list of all parents of the object to which you really want to dispatch.
Created by Simon Cozens, "simon@cpan.org". Copyright (c) 2003 Simon Cozens.
Maintained by chromatic, <chromatic at wgz dot org> after version 1.01. Copyright (c) 2004-2019 chromatic.
Thanks to Joshua ben Jore for bug reports and suggestions.
You may use and distribute this silly little module under the same terms as Perl itself.
2022-06-17 | perl v5.34.0 |