ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
12.5. Determining the Caller's PackageProblemYou need to find out the current or calling package. DiscussionThe print "I am in package __PACKAGE__\n"; # WRONG! I am in package __PACKAGE__ Needing to figure out the caller's package arose more often in older code that received as input a string of code to be package Alpha; runit('$line = <TEMP>'); package Beta; sub runit { my $codestr = shift; eval $codestr; die if $@; } Because package Beta; sub runit { my $codestr = shift; my $hispack = caller; eval "package $hispack; $codestr"; die if $@; } That approach only works when package Alpha; runit( sub { $line = <TEMP> } ); package Beta; sub runit { my $coderef = shift; &$coderef(); } This not only works with lexicals, it has the added benefit of checking the code's syntax at compile time, which is a major win. If all that's being passed in is a filehandle, it's more portable to use the Here's an example that reads and returns n lines from a filehandle. The function qualifies the handle before working with it. open (FH, "< /etc/termcap")
or die "can't open /etc/termcap: $!";
($a, $b, $c) = nreadline(3, 'FH');
use Symbol ();
use Carp;
sub nreadline {
my ($count, $handle) = @_;
my(@retlist,$line);
croak "count must be > 0" unless $count > 0;
$handle = Symbol::qualify($handle, ( If everyone who called your See AlsoThe documentation for the standard Symbol module, also found in Chapter 7 of Programming Perl; the descriptions of the special symbols |