1SUPER(3) User Contributed Perl Documentation SUPER(3)
2
3
4
6 SUPER - control superclass method dispatch
7
9 Find the parent method that would run if this weren't here:
10
11 sub my_method
12 {
13 my $self = shift;
14 my $super = $self->super('my_method'); # Who's your daddy?
15
16 if ($want_to_deal_with_this)
17 {
18 # ...
19 }
20 else
21 {
22 $super->($self, @_)
23 }
24 }
25
26 Or Ruby-style:
27
28 sub my_method
29 {
30 my $self = shift;
31
32 if ($want_to_deal_with_this)
33 {
34 # ...
35 }
36 else
37 {
38 super;
39 }
40 }
41
42 Or call the super method manually, with respect to inheritance, and
43 passing different arguments:
44
45 sub my_method
46 {
47 my $self = shift;
48
49 # parent handles args backwardly
50 $self->SUPER( reverse @_ );
51 }
52
54 When subclassing a class, you occasionally want to dispatch control to
55 the superclass -- at least conditionally and temporarily. The Perl
56 syntax for calling your superclass is ugly and unwieldy:
57
58 $self->SUPER::method(@_);
59
60 especially when compared to its Ruby equivalent:
61
62 super;
63
64 It's even worse in that the normal Perl redispatch mechanism only
65 dispatches to the parent of the class containing the method at compile
66 time. That doesn't work very well for mixins and roles.
67
68 This module provides nicer equivalents, along with the universal method
69 "super" to determine a class' own superclass. This allows you to do
70 things such as:
71
72 goto &{$_[0]->super('my_method')};
73
74 if you don't like wasting precious stack frames. (Because "super"
75 returns a coderef, much like "can" in UNIVERSAL, this doesn't break
76 "use strict 'refs'".)
77
78 If you are using roles or mixins or otherwise pulling in methods from
79 other packages that need to dispatch to their super methods, or if you
80 want to pass different arguments to the super method, use the "SUPER()"
81 method:
82
83 $self->SUPER( qw( other arguments here ) );
84
86 This module provides the following functions and methods:
87
88 "super()"
89 This function calls the super method of the currently-executing
90 method, no matter where the super method is in the hierarchy.
91
92 This takes no arguments; it passes the same arguments passed to the
93 currently-executing method.
94
95 The module exports this function by default.
96
97 Note: you must have the appropriate "package" declaration in place
98 for this to work. That is, you must have compiled the method in
99 which you use this function in the package from which you want to
100 use it. Them's the breaks with Perl 5.
101
102 "find_parent( $class, $method, $prune, $invocant )"
103 Attempts to find a parent implementation of $method starting with
104 $class. If you pass $prune, it will not ignore the method found in
105 that package, if it exists there. Pass $invocant if the object
106 itself might have a different idea of its parents.
107
108 The module does not export this function by default. Call it
109 directly.
110
111 "get_all_parents( $invocant, $class )"
112 Returns all of the parents for the $invocant, if it supports the
113 "__get_parents()" method or the contents of @ISA for $class. You
114 probably oughtn't call this on your own.
115
116 "SUPER()"
117 Calls the super method of the currently-executing method. You can
118 pass arguments. This is a method.
119
121 Beware: if you do weird things with code generation, be sure to name
122 your anonymous subroutines. See Perl Hacks #57.
123
124 Using "super" doesn't let you pass alternate arguments to your
125 superclass's method. If you want to pass different arguments, use
126 "SUPER" instead. D'oh.
127
128 This module does a small amount of Deep Magic to find the arguments of
129 method calling "super()" itself. This may confuse tools such as
130 "Devel::Cover".
131
132 In your own code, if you do complicated things with proxy objects and
133 the like, define "__get_parents()" to return a list of all parents of
134 the object to which you really want to dispatch.
135
137 Created by Simon Cozens, "simon@cpan.org". Copyright (c) 2003 Simon
138 Cozens.
139
140 Maintained by chromatic, <chromatic at wgz dot org> after version 1.01.
141 Copyright (c) 2004-2009 chromatic.
142
143 Thanks to Joshua ben Jore for bug reports and suggestions.
144
146 You may use and distribute this silly little module under the same
147 terms as Perl itself.
148
149
150
151perl v5.12.0 2010-05-06 SUPER(3)