greatshots has asked for the wisdom of the Perl Monks concerning the following question:

monks,

I would like to make my perl code to be portable to most of the OS's, I executed a following piece of code today and I have a doubt in the output which I got.

perl -e 'print "$0\n"' Output:- -e
$0 - should print the program name. correct me if I am wrong. why it has printed '-e'. does $0 take effect when I use it inside a perl's code? ( I mean not from command line )

Replies are listed 'Best First'.
Re: Reliability on $0
by davidrw (Prior) on Nov 07, 2006 at 02:57 UTC
    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]$
Re: Reliability on $0
by GrandFather (Saint) on Nov 07, 2006 at 02:52 UTC

    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
Re: Reliability on $0
by andyford (Curate) on Nov 07, 2006 at 11:03 UTC
    Not exactly an answer to portability, but as you start using $0, watch out for modules that change it.

    andyford
    or non-Perl: Andy Ford

Re: Reliability on $0
by holcapek (Sexton) on Nov 07, 2006 at 08:15 UTC
    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.

      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 -
Re: Reliability on $0
by smokemachine (Hermit) on Nov 07, 2006 at 17:24 UTC
    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...
      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 )
      -- 6x9=42