August 21, 2002

SUPER gotcha

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!

(Originally posted elsewhere)

Next: True
Previous: Smart, funny guy