So, I made a file named "print" with print "just another perl hacker\n"; in it...and went to work. First I tried: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`;
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:do print; 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; do (print); print "\n"; print $!, "\n" if $!;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |