Front page | perl.perl5.porters |
Postings from February 2000
local *UNIVERSAL::method = sub {} - odd behaviour
Thread Previous
From:
Nick Ing-Simmons
Date:
February 21, 2000 13:28
Subject:
local *UNIVERSAL::method = sub {} - odd behaviour
Message ID:
E12N0Lx-00027j-00@mserv1c.u-net.net
The notion of
local *UNIVERSAL::method = sub {};
struck me while I was reading Damian's book over the weekend.
(As a way of providing a fallback 'default:' in an OO 'case'.)
I am not surprised it 'works' but I am slightly suprised it 'lingers'
the way it does:
#!perl -x
my $obj = bless {},'Foo';
sub harry
{
local *UNIVERSAL::method = sub { print 'harry ',shift(@_),' ',join(',',caller),"\n" };
Thing->method;
$obj->method;
}
sub fred
{
local *UNIVERSAL::method = sub { print 'fred ',shift(@_),' ',join(',',caller),"\n" };
Thing->method;
$obj->method;
}
harry();
Thing->method; # no error !
$obj->method;
fred(); # does get new method!
Thing->method;
harry();
Thing->method;
__END__
Yields:
nick@bactrian:/home/perl > perl locuniv
harry Thing main,locuniv,7
harry Foo=HASH(0x80de9dc) main,locuniv,8
harry Thing main,locuniv,19
harry Foo=HASH(0x80de9dc) main,locuniv,20
fred Thing main,locuniv,14
fred Foo=HASH(0x80de9dc) main,locuniv,15
fred Thing main,locuniv,22
harry Thing main,locuniv,7
harry Foo=HASH(0x80de9dc) main,locuniv,8
harry Thing main,locuniv,24
nick@bactrian:/home/perl >
You don't get any errors outside the scope of the local - it re-uses the
last one - presumably because CV has been cached in Foo::method
and Thing::method. But if that is the case why does the re-definition
in fred's local "work" and not call the cached CV? (glad it does!).
Could/should the scope exit be tweaked to make it behave as I expect ?
--
Nick Ing-Simmons
Thread Previous