M
Matthew Braid
Hi all,
I've got an object-package which inherits from another. Simple enough.
Both use AUTOLOAD to catch accessor calls (see example below), the
'child' package using eval to dispatch anything it doesn't override to
its parent.
This works, but only if I use the 'stringy' version of eval (eg eval
"This is slow").
Is there an alternate way I can call a class' SUPER with a variable
function call? (The code is probably clearer than my attempt to explain
it
)
TIA,
MB
----EXAMPLE CODE----
package Foo;
sub new {
my $class = shift;
my $self = bless({}, (ref($class) or $class or __PACKAGE__));
$self->{bar} = "BAR!";
return $self;
}
sub AUTOLOAD {
my $self = shift;
my $var = $AUTOLOAD;
$var =~ s/^.*:://;
return if $var eq 'DESTROY';
if (exists $self->{$var}) {
print "AUTOLOADED $var - $self->{$var}\n";
} else {
die "Unknown accessor $AUTOLOAD";
}
}
package Baz;
use base qw/Foo/;
sub AUTOLOAD {
my $self = shift;
my $var = $AUTOLOAD;
$var =~ s/^.*:://;
return if $var eq 'DESTROY';
if ($var eq 'bar') {
# Add to parent's behaviour
print "BAZ AUTOLOADED $var\n";
}
my $ret;
# EEK - String eval
eval "\$ret = \$self->SUPER::$var(\@_)";
# Unfortunately this:
# eval {$ret = $self->SUPER::$var(@_)};
# does not even compile.
die $@ if $@;
return $ret;
}
package main;
my $baz = Baz->new;
$baz->bar;
----END EXAMPLE CODE----
I've got an object-package which inherits from another. Simple enough.
Both use AUTOLOAD to catch accessor calls (see example below), the
'child' package using eval to dispatch anything it doesn't override to
its parent.
This works, but only if I use the 'stringy' version of eval (eg eval
"This is slow").
Is there an alternate way I can call a class' SUPER with a variable
function call? (The code is probably clearer than my attempt to explain
it
TIA,
MB
----EXAMPLE CODE----
package Foo;
sub new {
my $class = shift;
my $self = bless({}, (ref($class) or $class or __PACKAGE__));
$self->{bar} = "BAR!";
return $self;
}
sub AUTOLOAD {
my $self = shift;
my $var = $AUTOLOAD;
$var =~ s/^.*:://;
return if $var eq 'DESTROY';
if (exists $self->{$var}) {
print "AUTOLOADED $var - $self->{$var}\n";
} else {
die "Unknown accessor $AUTOLOAD";
}
}
package Baz;
use base qw/Foo/;
sub AUTOLOAD {
my $self = shift;
my $var = $AUTOLOAD;
$var =~ s/^.*:://;
return if $var eq 'DESTROY';
if ($var eq 'bar') {
# Add to parent's behaviour
print "BAZ AUTOLOADED $var\n";
}
my $ret;
# EEK - String eval
eval "\$ret = \$self->SUPER::$var(\@_)";
# Unfortunately this:
# eval {$ret = $self->SUPER::$var(@_)};
# does not even compile.
die $@ if $@;
return $ret;
}
package main;
my $baz = Baz->new;
$baz->bar;
----END EXAMPLE CODE----