Calling subroutines with an ampersand but without parentheses is a special invocations that preserves @_ and should only be used in special advanced situations. This is not one of those.

You'd be better off writing POSIX::O_RDWR() or (if you used strict) POSIX::O_RDWR. Some information related to such choices is summarized in (tye)Re: A question of style.

Update: For those players at home confused by this answer (thinking I got my advice backward as far as strict.pm is concerned, one of whom was kind enough to /tell me about it)... The point of strict.pm is pretty much to catch simple mistakes, including typos.

Let's take two simple mistakes as examples: 1) you mispelt "O_RDWR" or 2) forgot to "use POSIX". Now consider these correct usages:

perl -le " use POSIX; print 'x',POSIX::O_RDWR();" perl -le "use strict; use POSIX; print 'x',POSIX::O_RDWR();" perl -le " use POSIX; print 'x',POSIX::O_RDWR;" perl -le "use strict; use POSIX; print 'x',POSIX::O_RDWR;"
which gives us results similar to the following:
> perl -le " use POSIX; print 'x',POSIX::O_RDWR();" x2 > perl -le "use strict; use POSIX; print 'x',POSIX::O_RDWR();" x2 > perl -le " use POSIX; print 'x',POSIX::O_RDWR;" x2 > perl -le "use strict; use POSIX; print 'x',POSIX::O_RDWR;" x2
Now consider these mistakes:
perl -le " print 'x',POSIX::O_RDWR();" perl -le " use POSIX; print 'x',POSIX::O_RDWT();" perl -le "use strict; print 'x',POSIX::O_RDWR();" perl -le "use strict; use POSIX; print 'x',POSIX::O_RDWT();" perl -le "use strict; print 'x',POSIX::O_RDWR;" perl -le "use strict; use POSIX; print 'x',POSIX::O_RDWT;"
which are all caught for us as follows:
> perl -le " print 'x',POSIX::O_RDWR();" Undefined subroutine &POSIX::O_RDWR called at -e line 1. > perl -le " use POSIX; print 'x',POSIX::O_RDWT();" O_RDWT is not a valid POSIX macro at -e line 1 > perl -le "use strict; print 'x',POSIX::O_RDWR();" Undefined subroutine &POSIX::O_RDWR called at -e line 1. > perl -le "use strict; use POSIX; print 'x',POSIX::O_RDWT();" O_RDWT is not a valid POSIX macro at -e line 1 > perl -le "use strict; print 'x',POSIX::O_RDWR;" Bareword "POSIX::O_RDWR" not allowed while "strict subs" in use at -e +line 1. Execution of -e aborted due to compilation errors. > perl -le "use strict; use POSIX; print 'x',POSIX::O_RDWT;" Bareword "POSIX::O_RDWT" not allowed while "strict subs" in use at -e +line 1. Execution of -e aborted due to compilation errors.
and finally consider these mistakes:
perl -le " print 'x',POSIX::O_RDWR;" perl -le " use POSIX; print 'x',POSIX::O_RDWT;"
which are not caught for us:
> perl -le " print 'x',POSIX::O_RDWR;" xPOSIX::O_RDWR > perl -le " use POSIX; print 'x',POSIX::O_RDWT;" xPOSIX::O_RDWT
and you see why I say you should only write POSIX::O_RDWR if you used strict (because if you try to write POSIX::O_RDWR without using strict.pm, you aren't protected from a simple mistake going unreported).

                - tye

In reply to Re: Ampersand prefix for package constants ? (don't) by tye
in thread Ampersand prefix for package constants ? by Anonymous Monk

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.