I'm guessing the braces localize what I'm doing, so once outside them, I can do whatever I want.
Be careful. The braces ({}) don't localize everything you are doing. The create a scope for your variables, but your package and subroutine declarations remain global. I think the form you used above gives a misleading impression that the package is block scoped.
Update 2: Thanks for the heaping helping of crow, ikegami--I needed it. I was wrong. Dead wrong. See package. I read this wrong information somewhere, failed to check it, and have promulgated bogus information as a result.
{ package Foo;
# Foo stuff
# goes in here.
}
sub FooFunc {
# This function is Foo::FooFunc!
#actually it's not! It is in main. I was wrong.
}
package main;
sub MainFunc {
# This function is main::MainFunc.
}
That's the reason why I like this approach better:
package Foo;
{ # Foo stuff
# goes in here.
}
package main;
# back in main.
Update: After looking at the module, I'd be inclined to use ikegami's suggestion for using a localized override.
This will minimize the spooky action at a distance factor.
#!/usr/bin/perl
use strict;
use warnings;
package Foo;
sub method {
routine();
}
sub routine {
return __PACKAGE__;
}
package Foo::Bar;
our @ISA = qw( Foo );
sub routine {
return __PACKAGE__;
}
sub method {
no warnings 'redefine';
# Temporarily override routine() in the parent class.
local *Foo::routine = \&routine;
# Delegate to parent method.
my $self = shift;
$self->SUPER::method(@_);
}
package main;
print "Foo: ", Foo->method, "\n";
print "Foo::Bar: ", Foo::Bar->method, "\n";
print "Foo: ", Foo->method, "\n";
|