Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

lachoy (1663)

lachoy
  chris.winters@gmail.com
http://www.cwinters.com/

I am actually Chris Winters; I am actually living in Pittsburgh, Pennsylvania, USA; I am actually married and have three cats. (Guess what one of them is named?) I am the "OpenInteract" guy, which could be good or bad.

Journal of lachoy (1663)

Wednesday August 21, 2002
09:09 AM

SUPER gotcha

[ #7212 ]

Rule for the day: pay attention to the docs! I was wondering why SUPER wasn't working in generated classes/subs, illustrated by:

use strict;

package Foo;
sub save { return "saved!" }

package Bar;
@Bar::ISA = qw( 'Foo' );

package main;

sub create_sub {
    my ( $class, $method ) = @_;
    no strict 'refs';
    *{ $class . '::' . $method } =
      sub {
          my ( $class ) = @_;
          return $class->SUPER::save();
      };
}

create_sub( 'Bar', 'save' );
print Foo->save(), "\n";
print Bar->save(), "\n";

Which prints:

>perl test_super.pl
saved!
Can't locate object method "save" via package "main" (perhaps you forgot to load "main"?) at test_super.pl line 21.

SUPER can only be used within the class, which makes me suspect that it's a compile-time directive. Or, as the docs (perlobj) state:

If you're trying to control where the method search begins and you're executing in the class itself, then you may use the SUPER pseudo class, which says to start looking in your base class's @ISA list without having to name it explicitly:

And one brief code example later there's another warning:

Please note that the SUPER:: construct is meaningful only within the class.

Oh well!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.