= Avoiding use Module

> 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.

= Regarding the magic filehandle _

I didn't know (anymore?) about This, and it scares me a little bit, that it has such global effects.

- The main namespace

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

- Workaround

As a practical solution, I'd say avoid _ and take double __ when operating in main.

- Documentation

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 _

- Deprecation?

the last paragraph makes we wondering if we can't slowly deprecate such global side effects.

Cheers Rolf

( addicted to the Perl Programming Language)


In reply to Re: Prevent Strings From Being Interpreted As A File Handle by LanX
in thread Prevent Strings From Being Interpreted As A File Handle by amon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.