develooper Front page | perl.perl5.porters | Postings from February 2015

Re: [perl #123775] Args to 'system' not evaluated before forking,making $$ wrong

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
February 11, 2015 11:34
Subject:
Re: [perl #123775] Args to 'system' not evaluated before forking,making $$ wrong
Message ID:
20150211112848.GZ15494@iabyn.com
On Mon, Feb 09, 2015 at 01:56:59PM -0800, via RT wrote:
> The original problem which led me to this was:
> 
>   system "ps","-p",$$,"-o","pid,etime,bsdtime,c,rss";  # always shows wrong values
> 
> The problem appears to be that $$ is not correct when passed to system().
> I'm guessing it evaluates to the pid of an internaly-created child process.
> 
> Can 'system' (and any other forking operators) be made to
> evaluate their arguments *before* forking?

That will also affect things like tied vars, which are currently also
evaluated in the child:

    sub TIESCALAR { bless [] }
    sub FETCH { print "FETCH: pid=$$\n"; 1 }
    my $x;
    tie $x, 'main';
    print "parent: pid=$$\n";
    system "echo", "child: x=", $x;

which outputs:

    parent: pid=4773
    FETCH: pid=4774
    child: x= 1

I suspect that evaluating in the parent is The Right Thing to Do, but
worry about backwards compatibility.


-- 
A walk of a thousand miles begins with a single step...
then continues for another 1,999,999 or so.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About