Mon Aug 17 15:12:38 2009: Request 48770 was acted upon.
Transaction: Correspondence added by sch...@argumentum.de
       Queue: PAR
     Subject: Re: [rt.cpan.org #48770] $0 is not working when PAR is used
   Broken in: (no value)
    Severity: (no value)
       Owner: Nobody
  Requestors: mianro...@hotmail.com
      Status: new
 Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=48770 >


2009/8/17 Miguel Angel Rojas Gómez via RT <bug-...@rt.cpan.org>:
>      It seems that $0 var is not processed properly inside PAR binaries:

It is. Your notion of "properly" is wrong :)

>From the Perl source (perl.c):

        /* Set PL_origalen be the sum of the contiguous argv[]
         * elements plus the size of the env in case that it is
         * contiguous with the argv[].  This is used in mg.c:Perl_magic_set()
         * as the maximum modifiable length of $0.  In the worst case
         * the area we are able to modify is limited to the size of
         * the original argv[0].  (See below for 'contiguous', though.)

Try the following script (essentially your example):

print "before \$0=$0\n";
$0 = "12345678901234567890";
print "after \$0=$0\n";
system qw(ps -f), $$;

OK, run it:

$ perl foo.pl
before $0=foo.pl
after $0=12345678901234567890
UID        PID  PPID  C STIME TTY      STAT   TIME CMD
roderich 17517 28077  0 21:04 pts/3    S+     0:00 12345678901234567890

But this only works because most environments are large enough so that
the new value of $0 will fit in to the combined contiguous arg+env space.
If we shrink the environment to nothing it doesn't work anymore:

$  env - perl foo.pl
before $0=foo.pl
after $0=12345678901234567890
UID        PID  PPID  C STIME TTY      STAT   TIME CMD
roderich 17647 28077  0 21:06 pts/3    S+     0:00 12345678901

The same happens in a packed executable because it already manipulates
$0 (and @ARGV and the environment) before it even gets to execute
your script. Afterwards the "combined contiguous"
space has shrunk to the size of $0 itself and that's what you observe.

Sorry, no cake.

Cheers, Roderich

Reply via email to