To recap, the construct do expression is taking the bareword print as a string, not as a call to the built-in function.

Even more curiously, do 4; produces 1 as a result with nothing in any of the error variables, as opposed to do '4'; which looks for a file. Ditto for do 2+2 which makes me think that a numeric value is being treated differently. I don't even know what it's doing! Could it be a bug, due to common code in the implementation with use and require, which indeed mean something different with numbers? Update: yes, it is doing exactly that.

As for barewords, others like do shift; is looking at is as a string, not a function, too.

My guess is that, contrary to the documentation, a bareword to do is always treated as a string (even under use strict and use warnings!) like with use.

You may want to try and see if it replaces :: with slash, like require does. It's not assuming the extension, so maybe not. But it sounds like what's happening is do/require/use is implementing some functionality at the do level, or the compiler has more in common with them than is documented.

Hopefully, a perl porter will investigate this further. I agree, it is not behaving as documented in perlfunc and perldata. My guess: common handling with use/do/require in the parser. I'd say document the "always a string", and fix the numeric context behavior to take it as a string (it's pointless to duplicate require).

Now, for the parens: That makes it a list. In scalar context, it gives the item count. Anything that's not a bareword is treated as a normal expression, and if non-numeric, the result of the expression names the file. —John


In reply to Re: The Behavior of 'do' by John M. Dlugosz
in thread The Behavior of 'do' by dsb

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.