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.
75
76 If you are using roles or mixins or otherwise pulling in methods from
77 other packages that need to dispatch to their super methods, or if you
78 want to pass different arguments to the super method, use the SUPER()
79 method:
80
81 $self->SUPER( qw( other arguments here ) );
82
84 This module provides the following functions and methods:
85
86 super()
87 This function calls the super method of the currently-executing
88 method, no matter where the super method is in the hierarchy.
89
90 This takes no arguments; it passes the same arguments passed to the
91 currently-executing method.
92
93 The module exports this function by default.
94
95 Note: you must have the appropriate "package" declaration in place
96 for this to work. That is, you must have compiled the method in
97 which you use this function in the package from which you want to
98 use it. Them's the breaks with Perl 5.
99
100 "find_parent( $class, $method, $prune, $invocant )"
101 Attempts to find a parent implementation of $method starting with
102 $class. If you pass $prune, it will not ignore the method found in
103 that package, if it exists there. Pass $invocant if the object
104 itself might have a different idea of its parents.
105
106 The module does not export this function by default. Call it
107 directly.
108
109 "get_all_parents( $invocant, $class )"
110 Returns all of the parents for the $invocant, if it supports the
111 "__get_parents()" method or the contents of @ISA for $class. You
112 probably oughtn't call this on your own.
113
114 SUPER()
115 Calls the super method of the currently-executing method. You can
116 pass arguments. This is a method.
117
119 Beware: if you do weird things with code generation, be sure to name
120 your anonymous subroutines. See Perl Hacks #57.
121
122 Using "super" doesn't let you pass alternate arguments to your
123 superclass's method. If you want to pass different arguments, use
124 "SUPER" instead. D'oh.
125
126 This module does a small amount of Deep Magic to find the arguments of
127 method calling super() itself. This may confuse tools such as
128 "Devel::Cover".
129
130 In your own code, if you do complicated things with proxy objects and
131 the like, define "__get_parents()" to return a list of all parents of
132 the object to which you really want to dispatch.
133
135 Created by Simon Cozens, "simon@cpan.org". Copyright (c) 2003 Simon
136 Cozens.
137
138 Maintained by chromatic, <chromatic at wgz dot org> after version 1.01.
139 Copyright (c) 2004-2019 chromatic.
140
141 Thanks to Joshua ben Jore for bug reports and suggestions.
142
144 You may use and distribute this silly little module under the same
145 terms as Perl itself.
146
147
148
149perl v5.36.0 2023-01-20 SUPER(3)