note
tobyink
<p>Yup - and you don't even need to do so at the "top level". <c>package</c> statements can appear within blocks. Like...</p>
<c>
package main;
use strict;
use warnings;
{
package Person;
use Data::Dumper (); # load, but import nothing!
sub new {
my $class = shift;
bless {@_}, $class;
}
sub dump {
my $self = shift;
package Data::Dumper;
# The following line is executed in Data::Dumper,
# so it can see the "Dumper" sub defined there!
print Dumper($self);
}
# We're back to the Person package here because
# the closing brace ended the Data::Dumper package
printf "Line %d is in package %s\n", __LINE__, __PACKAGE__;
}
# And now we're back to the main package.
printf "Line %d is in package %s\n", __LINE__, __PACKAGE__;
my $bob = Person->new(name => 'Robert');
$bob->dump;
</c>
<div class="pmsig"><div class="pmsig-757127">
<small><small><tt>use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name</tt></small></small>
</div></div>
1074633
1074661