Interesting. I just tried something similar, showing the value of $0
before invoking ps
.
Here's my script:
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
print "\$0 = $0\n";
system 'ps', "ww$$";
print "\n";
{
local $0 = 'foo';
print "\$0 = $0\n";
system 'ps', "ww$$";
print "\n";
}
print "\$0 = $0\n";
system 'ps', "ww$$";
and here's the output (the first $
is a shell prompt):
$ ./foo.pl
$0 = ./foo.pl
PID TTY STAT TIME COMMAND
2478 pts/2 S+ 0:00 /usr/bin/perl ./foo.pl
$0 = foo
PID TTY STAT TIME COMMAND
2478 pts/2 S+ 0:00 foo
$0 = ./foo.pl
PID TTY STAT TIME COMMAND
2478 pts/2 S+ 0:00 ./foo.pl
So $0
is restored to its original value, but the information printed by ps
is different.
Note that @EricStrom's workaround doesn't quite have the (presumably) intended result; it makes ps
show the command name as perl
, not /usr/bin/perl
(the latter could be useful information).
It appears that Perl is attempting to restore $0
to its original value, but it doesn't quite succeed in restoring the information shown by ps
.
I think the bottom line is that modifying $0
can be useful, but the behavior is not 100% reliable or portable.
perldoc perlvar
says:
On some (but not all) operating systems assigning to $0 modifies the
argument area that the ps program sees. On some platforms you may have
to use special ps options or a different ps to see the changes.
Modifying the $0 is more useful as a way of indicating the current
program state than it is for hiding the program you're running.