yes .. with -e you aren't running a program, you're executing a "line" of code, so $0 doesn't really have a value, which makes sense (setting it to 'perl' won't be useful since obviously perl is running). If you save it to a script, it will show that scriptname ..
[me@host]$ echo 'print "$0\n";' > /tmp/p
[me@host]$ perl /tmp/p
/tmp/p
[me@host]$
| [reply] [d/l] |
According to perlvar $0 is the name of the program that is being executed - that is, the name of the Perl script. "-e" seems entirely sensible when the script is included on the command line in that fashion.
DWIM is Perl's answer to Gödel
| [reply] |
Not exactly an answer to portability, but as you start using $0, watch out for modules that change it.
| [reply] |
Maybe you're wondering how could you determine if you're running one-liner (-e option) or script actually (-e is a bit strange name for a script, but it can happen) :-)
If it's the case, simply test if -f $0: if true, you're running a script, otherwise it's one-liner. | [reply] [d/l] |
If it's the case, simply test if -f $0: if true, you're running a script, otherwise it's one-liner.
Most certainly not! You are already assuming that a file with the name -e can exist (otherwise, your proposed test would be pointless). But the existance of a file named -e doesn't prohibit me from executing perl -e.
But there are other reasons it might fail. The program may have dropped its priviledges, and it now can now longer read the directory the script resides in, so -f would return false. Or the script may have disappeared, or it may have been moved between the start of the program, and the execution of the -f test.
Furthermore, beside the magical value -e, there's also the magical value -, when the program is gotton from STDIN.
$ echo 'print $0' | perl -l
-
| [reply] [d/l] |
a file named -e can exist! and can be used by perl... the -f realy can answer if this is a file or not... to use the file named you can use perl -- -e
like:
echo 'print "Is a file" if -f $0' > -e
perl -- -e prints "Is a file"
and perl -e 'print "Is a file" if -f $0' don't... | [reply] [d/l] [select] |
Both commands print "Is a file"
Even if you use "perl -e", the -e file still exists (i didn't saw a "rm -- -e" in your post )
| [reply] |