Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

$0 inconsistency?

by misterperl (Pilgrim)
on Apr 13, 2022 at 13:35 UTC ( [id://11142953]=perlquestion: print w/replies, xml ) Need Help??

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

I have a statement like :

print F "$0:$t\n";

SOMETIMES it prints:
myprog.pl:2
and other times the same exact statement prints:
/home/user/perlcode/myprog.pl:2

Which is unexpected, fairly annoying, and inconvenient , since later when I grep the file for $0, SOMETIMES the path is there, sometimes not. And which variety is used in the grep is also consequential, and indeterminate. Is there some other perlvar, or some context, where $0 would include the path, and sometimes not? Perl v5.16.3

Also, on other nodes (dev and qa) this code worked 100% - no paths. This all started when the code was copied to another CENTOS node.

TY Perl Dieties esp Damian!

Replies are listed 'Best First'.
Re: $0 consistent!
by LanX (Saint) on Apr 13, 2022 at 13:47 UTC
    It's exactly what you ran, which depends on your current working directory.

    d:\tmp>echo print $0 > test.pl d:\tmp>perl test.pl test.pl d:\tmp>cd .. d:\>perl tmp/test.pl tmp/test.pl d:\>perl tmp\test.pl tmp\test.pl d:\>perl tmp/../tmp\test.pl tmp/../tmp\test.pl d:\>

    update

    it's verbatim what was called, even the slashes are not normalized on windows.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      OK so when I call it I use perl -wd myscript.pl $0 has no path, and when the crontab calls it, its the full path..

      Rolf- you are BRILL! TY the world makes sense again..

Re: $0 inconsistency?
by choroba (Cardinal) on Apr 13, 2022 at 17:08 UTC
    If your tooling depends on the value of $0, note that $0 can be changed.
    { local $0 = 'my program'; print $0; # my program system 'ps'; # <- works even here, at least on my Linux box. } print $0; # Back to the old values. system 'ps'; # ditto
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Eh... not quite. Setting argv[0] in the process should work on most POSIX systems. That change is an additional thing it does for Linux, and notes the limit for that.

        This is on a Mac.:

        $ perl -e '$0="my really awesome process name"; sleep 60;' & [1] 73413 $ ps PID TTY TIME CMD 70164 ttys002 0:00.05 /opt/local/bin/bash 677 ttys004 0:05.06 /opt/local/bin/bash 73413 ttys004 0:00.03 my really awesome process name
      tyvm I just copied it in case somewhere else it expected it otherwise:
      $_=$0; s|.+/||;
      Cheers thx for the thoughtful reply!
Re: $0 inconsistency?
by Perlbotics (Archbishop) on Apr 13, 2022 at 17:59 UTC

    Sometimes, this is useful like ($me) for usage info (usage: $me [ARGS]).

    use strict; use warnings; use Path::Tiny (); my $me = Path::Tiny::path( $0 )->basename(); my $abs_me = Path::Tiny::path( $0 )->realpath(); print "1) original: $0\n"; print "2) short : $me\n"; print "3) long : $abs_me\n";

    Result:

    rp@linux:local: perl ../me.pl 1) original: ../me.pl 2) short : me.pl 3) long : /home/rp/me.pl

Re: $0 inconsistency? $0 doc
by Anonymous Monk on Apr 13, 2022 at 13:45 UTC
      you should try to check the validity of your links in the preview before submitting.

      pity, good list otherwise.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        you should try to check the validity of your links in the preview before submitting. pity, good list otherwise.

        i did check. But i did miss one. Obviously this is a job for computers not fingers on a touchscreen

        TY all experts for your kind and gentle replies. Despite Perl programming since 1995, and hours with Damian, MJD, Larry et al, amazingly, sometimes simple things like this confound me. Expseically little-used ones like this.

        To all a Happy holiday or at least a great weekend :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11142953]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2024-04-25 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found