package Blah; {
sub foo {
print "bar!\n";
}
}
package main; {
foo();
}
This code will define foo() in the Blah package; it will not be available in the main package unless exported to it or invoked as a fully qualified subroutine (called without warnings or strictures):
c:\@Work\Perl\monks\R0b0t1>perl -le
"package Blah; {
sub foo {
print \"bar!\n\";
}
}
package main; {
foo();
}
"
Undefined subroutine &main::foo called at -e line 1.
c:\@Work\Perl\monks\R0b0t1>perl -le
"package Blah; {
sub foo {
print \"bar!\n\";
}
}
package main; {
Blah::foo();
}
"
bar!
On a tangential note, the package syntax
{ package Foo;
my $x = ...;
...
sub bar { ... }
...
}
and from Perl version 5.14 onward
package Foo {
my $x = ...;
...
sub bar { ... }
...
}
will cause the asserted package to be "turned off" at the end of the block, with reversion to the "original" package. E.g., (
with full warnings and strictures):
c:\@Work\Perl\monks\R0b0t1>perl -wMstrict -le
"print 'perl version: ', $];
;;
in_pkg('A');
;;
package Foo {
::in_pkg('B');
}
;;
in_pkg('C');
;;
sub in_pkg { print qq{$_[0]: in package }, scalar caller; }
"
perl version: 5.014004
A: in package main
B: in package Foo
C: in package main
Give a man a fish: <%-{-{-{-<