Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

lying about program name

by japl (Initiate)
on Jun 14, 2018 at 02:35 UTC ( #1216607=perlquestion: print w/replies, xml ) Need Help??

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

perldoc on exec/system states that one can lie about name of the program being executed via indirect object syntax; but it doesn't appear so:

a simple test,
$ cat foo #!/usr/bin/perl -w use strict; print "I am $0 (@{[join ' ', @ARGV]})\n"; $ cat bar #!/usr/bin/perl -w use strict system { './foo' } 'bar', @ARGV; $ ./bar 1 2 3 I am ./foo (1 2 3) $
shouldn't it be "I am bar (1 2 3"?

Replies are listed 'Best First'.
Re: lying about program name
by hippo (Bishop) on Jun 14, 2018 at 10:47 UTC

    Typos aside, it is your understanding of the name change which does not tally. It is the name for the O/S which is being changed. Demo:

    #!/usr/bin/perl use strict; use warnings; print "I am $0 (@{[join ' ', @ARGV]})\n"; system ("ps $$"); shift; exit unless @ARGV; my @args = ('bar', 'foo.pl', @ARGV); system { '/usr/bin/perl' } @args;

    Running this I see:

    $ ./foo.pl 1 1 I am ./foo.pl (1 1) PID TTY STAT TIME COMMAND 3717 pts/0 SN+ 0:00 /usr/bin/perl ./foo.pl 1 1 I am foo.pl (1) PID TTY STAT TIME COMMAND 3719 pts/0 SN+ 0:00 bar foo.pl 1

    So the name in the O/S has changed to 'bar' but within the script, $0 is still foo.pl since that is the file which perl is executing. Additionally, if you call system ('./foo.pl') the O/S uses the hashbang line to re-exec perl with the script as argument (as in the initial invocation in my run above), so your aliasing there would have no effect even at the O/S level.

Re: lying about program name
by choroba (Archbishop) on Jun 14, 2018 at 12:49 UTC
    Also note that $0 is writable.
    say $0; $0 = 'python'; say $0;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: lying about program name
by taint (Chaplain) on Jun 14, 2018 at 06:25 UTC
    FWIW

    You didn't close your use statement on line 2 of bar

    #!/usr/bin/perl -w use strict; __________^ system { './foo' } 'bar', @ARGV;

    Which fixed it for me. :-)

    EDIT:
    Didn't -w scream at you?

    Evil is good, for without it, Good would have no value
    λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      that was probably due to my editing the post to remove some unwanted noise (ie. hostname, username etc.) >> Which fixed it for me. :-) you meant it actually printed out "I am bar (1 2 3)" as expected? (ie. $0 gets 'bar' instead of './foo') . what perl version did you use? i've tried this on all 5.16, 5.18 and 5.22 ... all the same. thanks for looking at it.
        Having only added the semicolon to the bar script. My copies were exactly as you posted them.

        Just for fun. Here's the output from my session:

        devel# ./bar Can't exec "./foo": No such file or directory at ./bar line 3. Unknown 'strict' tag(s) '-1' at ./bar line 3. BEGIN failed--compilation aborted at ./bar line 3. devel#
        Ahh, sure enough. No semicolon on line 2 -- gotta love that -w :-)
        devel# ./bar I am ./foo ()
        Looks good. Lets try it as intended
        devel# ./bar 123 I am ./foo (123) devel#
        I think I can safely reply now. :-)

        Evil is good, for without it, Good would have no value
        λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Re: lying about program name
by ikegami (Patriarch) on Jun 14, 2018 at 13:00 UTC

    The OS can't lie to perl as to which script to execute because Perl wouldn't be able to find the right script to execute, and $0 contains the name of the script passed to perl, so $0 must necessarily contains the correct name of the script.

Re: lying about program name
by Anonymous Monk on Jun 14, 2018 at 09:50 UTC
    What operating system?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-06-29 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (96 votes). Check out past polls.

    Notices?