I started playing around with <a href-"http://www.perlmonks.org/index.pl?node=perlfunc%3Ado&lastnode_id=395>do, and the docs say:
Uses the value of EXPR as a filename and executes the contents of the +file as a Perl script. Its primary use is to include subroutines from + a Perl subroutine library. do 'stat.pl'; is just like scalar eval `cat stat.pl`;
So, I made a file named "print" with print "just another perl hacker\n"; in it...and went to work. First I tried:
do ("print"); print $!, "\n" if $!;
Which worked as expected. "just another perl hacker" was the output. Then I tried:
do print; print $!, "\n" if $!;
I expected that the argument to do would be 1, since print with or without arguments evaluates to 1. So I was expecting a No such file or directory error from $!. Didn't happen. "just another perl hacker" was output as if do saw print as a string. Wondering if that was the case, I assigned a value to $_ since print assumes $_ as its argument when none is given:
$_ = 1; do print; print "\n"; print $!, "\n" if $!;
Output was "just another perl hacker". So print is being evaluated as a string I think. Just for kicks I throw some parens around it, thinking it won't matter(since it usually doesn't), but not really knowing what to expect:
$_ = 1; do (print); print "\n"; print $!, "\n" if $!;
Finally, print is evaluated down to 1, and do assigns the long awaited "No such file or directory error" to $!, so the output is:
1
No such file or directory

So, why was print evaluated as a string when not in parens? I was under the impression that Perl would recognize it as a built in function and act accordingly. Instead it acted as if it were a bareword, and there was know pre-defined sub-routine called print. And why, in this case is there a difference in the behavior with/without parentheses?

Thanks in advance.

Amel - f.k.a. - kel


In reply to 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.