dsb has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: The Behavior of 'do'
by trantor (Chaplain) on Jul 26, 2001 at 22:58 UTC | |
by tomazos (Deacon) on Jul 27, 2001 at 01:05 UTC | |
|
Re: The Behavior of 'do'
by PrakashK (Pilgrim) on Jul 26, 2001 at 23:00 UTC | |
|
Re: The Behavior of 'do'
by John M. Dlugosz (Monsignor) on Jul 26, 2001 at 23:20 UTC | |
|
Re: The Behavior of 'do'
by mitd (Curate) on Jul 26, 2001 at 23:18 UTC | |
by wine (Scribe) on Jul 26, 2001 at 23:45 UTC | |
by dsb (Chaplain) on Jul 26, 2001 at 23:52 UTC |