in reply to Prevent Strings From Being Interpreted As A File Handle
> As I want to prevent the “_” package from being used directly, I boobie-trapped the “_::import” method to blow with a helpful error message whenever touched.
this seems to be the real question and the answer was already given at stack overflow, don't boobie-trap the import but the module. Any required module is evaled via do FILE.
Just put your code before the package declaration and it's executed in the context of the caller.
> cat Module.pm script.pl; perl script.pl #---- Module.pm warn "NO NO NO"; package Module; sub whatever {} 1; #---- script.pl use Module; #---- output NO NO NO at Module.pm line 1.
I didn't know (anymore?) about This, and it scares me a little bit, that it has such global effects.
Please note, it always belongs to the '%main::' stash:
$\="\n"; package TST; print "exists main::_ ", exists $main::{_}; print -f 'Module.pm' && -w _; print "exists TST::_ ", exists $TST::{_}; print -w main::_;
# OUTPUT exists main::_ 1 1 exists TST::_ 1
As a practical solution, I'd say avoid _ and take double __ when operating in main.
Finding documentation in perldoc wasn't easy, thats what I detected in perlfunc
#5.10 If any of the file tests (or either the "stat" or "lsta +t" operators) are given the special filehandle consisting +of a solitary underline, then the stat structure of the prev +ious file test (or stat operator) is used, saving a system c +all. # ... special cases + example code ... # BUT ALSO As of Perl 5.9.1, as a form of purely syntactic sugar, +you can stack file test operators, in a way that "-f -w -x $fil +e" is equivalent to "-x $file && -w _ && -f _
the last paragraph makes we wondering if we can't slowly deprecate such global side effects.
Cheers Rolf
( addicted to the Perl Programming Language)
|
|---|