/usr/share/perl5/SUPER.pm is in libsuper-perl 1.20120705-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | # Dear PAUSE: please do not index this
package
DB;
sub uplevel_args { my @foo = caller(2); return @DB::args }
# Dear PAUSE: nor this
package
UNIVERSAL;
use strict;
use warnings;
use Scalar::Util 'blessed';
sub super
{
return ( SUPER::find_parent( @_, '', $_[0] ) )[0];
}
sub SUPER
{
my $self = $_[0];
my $blessed = blessed( $self );
my $self_class = defined $blessed ? $blessed : $self;
my ($class, $method) = ( caller( 1 ) )[3] =~ /(.+)::(\w+)$/;
my ($sub, $parent) =
SUPER::find_parent( $self_class, $method, $class, $self );
return unless $sub;
goto &$sub;
}
package SUPER;
{
$SUPER::VERSION = '1.20120705';
}
# ABSTRACT: control superclass method dispatch
use strict;
use warnings;
use base 'Exporter';
@SUPER::ISA = 'Exporter';
@SUPER::EXPORT = 'super';
use Carp;
use Scalar::Util 'blessed';
use Sub::Identify ();
sub find_parent
{
my ($class, $method, $prune, $invocant) = @_;
my $blessed = blessed( $class );
$invocant ||= $class;
$class = $blessed if $blessed;
$prune ||= '';
my @parents = get_all_parents( $invocant, $class );
# only check parents above the $prune point
my $i = $#parents;
for my $parent (reverse @parents) {
last if $parent eq $prune;
$i--;
}
for my $parent ( @parents[$i .. $#parents] )
{
if ( my $subref = $parent->can( $method ) )
{
my $source = Sub::Identify::sub_fullname( $subref );
next if $source eq "${prune}::$method";
return ( $subref, $parent );
}
}
}
sub get_all_parents
{
my ($invocant, $class) = @_;
my @parents = eval { $invocant->__get_parents() };
unless ( @parents )
{
no strict 'refs';
@parents = @{ $class . '::ISA' };
}
return 'UNIVERSAL' unless @parents;
return @parents, map { get_all_parents( $_, $_ ) } @parents;
}
sub super()
{
# Someone's trying to find SUPER's super. Blah.
goto &UNIVERSAL::super if @_;
@_ = DB::uplevel_args();
carp 'You must call super() from a method call' unless $_[0];
my $caller = ( caller(1) )[3];
my $self = caller();
$caller =~ s/.*:://;
goto &{ $self->UNIVERSAL::super($caller) };
}
1;
=head1 NAME
SUPER - control superclass method dispatch
=head1 SYNOPSIS
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 @_ );
}
=head1 DESCRIPTION
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 I<at compile time>. That doesn't work very well for mixins and roles.
This module provides nicer equivalents, along with the universal method
C<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. (Because C<super> returns a
coderef, much like L<UNIVERSAL/can>, this doesn't break C<use strict 'refs'>.)
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 C<SUPER()> method:
$self->SUPER( qw( other arguments here ) );
=head1 FUNCTIONS and METHODS
This module provides the following functions and methods:
=over
=item C<super()>
This function calls the super method of the currently-executing method, no
matter where the super method is in the hierarchy.
This takes no arguments; it passes the same arguments passed to the
currently-executing method.
The module exports this function by default.
I<Note>: you I<must> have the appropriate C<package> declaration in place for
this to work. That is, you must have I<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.
=item C<find_parent( $class, $method, $prune, $invocant )>
Attempts to find a parent implementation of C<$method> starting with C<$class>.
If you pass C<$prune>, it will not ignore the method found in that package, if
it exists there. Pass C<$invocant> if the object itself might have a different
idea of its parents.
The module does not export this function by default. Call it directly.
=item C<get_all_parents( $invocant, $class )>
Returns all of the parents for the C<$invocant>, if it supports the
C<__get_parents()> method or the contents of C<@ISA> for C<$class>. You
probably oughtn't call this on your own.
=item C<SUPER()>
Calls the super method of the currently-executing method. You I<can> pass
arguments. This is a method.
=back
=head1 NOTES
I<Beware:> if you do weird things with code generation, be sure to I<name> your
anonymous subroutines. See I<Perl Hacks> #57.
Using C<super> doesn't let you pass alternate arguments to your superclass's
method. If you want to pass different arguments, use C<SUPER> instead. D'oh.
This module does a small amount of Deep Magic to find the arguments of method
I<calling> C<super()> itself. This may confuse tools such as C<Devel::Cover>.
In your own code, if you do complicated things with proxy objects and the like,
define C<__get_parents()> to return a list of all parents of the object to
which you really want to dispatch.
=head1 AUTHOR
Created by Simon Cozens, C<simon@cpan.org>. Copyright (c) 2003 Simon Cozens.
Maintained by chromatic, E<lt>chromatic at wgz dot orgE<gt> after version 1.01.
Copyright (c) 2004-2012 chromatic.
Thanks to Joshua ben Jore for bug reports and suggestions.
=head1 LICENSE
You may use and distribute this silly little module under the same terms as
Perl itself.
|