SUPER - Control superclass method despatch
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 just, Ruby-style:
sub my_method { my $self = shift; if ($want_to_deal_with_this) { ... } else { super } }
When subclassing a class, you occasionally want to despatch 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 with its Ruby equivalent:
super;
This module provides that equivalent, along with the universal method super to determine one's own superclass. This allows you do to things like
super
goto &{$_[0]->super("my_method")};
if you don't like wasting precious stack frames. (And since super returns a coderef, much like "can" in UNIVERSAL, this doesn't break use strict 'refs'.)
use strict 'refs'
It has been pointed out that using super doesn't let you pass alternate arguments to your superclass's method. If you want to pass different arguments, well, don't use super then. D'oh.
This module does a small amount of Deep Magic to find out what arguments the method calling super itself had, and this may confuse things like Devel::Cover.
Devel::Cover
Simon Cozens, simon@cpan.org
simon@cpan.org
This silly little module may be distributed under the same terms as Perl itself.
To install UNIVERSAL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm UNIVERSAL
CPAN shell
perl -MCPAN -e shell install UNIVERSAL
For more information on module installation, please visit the detailed CPAN module installation guide.