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

dear monks,

perl -i.$$ -pe 's/search/replace/g' *.html
In the above code I would like to have PROCESS_ID of one line perl code as the backup file extension. but surprisingly, I am always getting the shell PID as the pid suffixed to the backup filename. how can I get the PID of one line perl code which got executed as above as the filename's extension?

Replies are listed 'Best First'.
Re: Backup filename extension with -i option
by ikegami (Patriarch) on Dec 26, 2006 at 09:13 UTC

    [Moved parent from CUFP to SoPW. Perl questions are to be posted in SoPW.]

    The command (including $$) is being parsed and interpreted by the shell. If you want Perl to interpret $$, it needs to be in the Perl code. Fortunately, inplace editing (-i) can be initiated from within a script.

    perl -pe 'BEGIN { $^I = ".$$" } s/search/replace/g' *.html

    Ref: $^I

Re: Backup filename extension with -i option
by ferreira (Chaplain) on Dec 26, 2006 at 14:48 UTC

    The remark done by ikegami becomes obvious if you peek the code behind the one-liner with B::Deparse:

    $ perl -MO=Deparse -i.$$ -pe 's/search/replace/g' *.html BEGIN { $^I = ".2412"; } LINE: while (defined($_ = <ARGV>)) { s/search/replace/g; } continue { print $_; } -e syntax OK

    In this case, the "2412" came from the shell as ikegami pointed. I tried to hide $$ from the shell by quoting, but to no avail. Indeed, $$ must be in the Perl code — the argument of -i is interpreted as literal string in attempts like:

    $ perl -MO=Deparse -i'.$$' -pe 's/search/replace/g' *.html BEGIN { $^I = ".\$\$"; } ...
Re: Backup filename extension with -i option
by jonadab (Parson) on Dec 26, 2006 at 15:05 UTC
    I would like to have PROCESS_ID of one line perl code as the backup file extension.

    One supposes that you are already aware that process IDs get re-used, so that this could easily lead to some of the files being overwritten. It scarcely seems necessary to mention that, since of course you surely were already aware of it, but there I go again, stating the obvious.


    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy.
Re: Backup filename extension with -i option
by f00li5h (Chaplain) on Dec 27, 2006 at 01:25 UTC

    UpdateThis was already covered in depth by the rest of the thread, it would be safe for you to ignore it.

    I think the reason you're geting the shell's pid is that the shell gets to the $$, sticks it's pid into there, and passes it's pid off to perl as the value of the argument to -i...

    If I ask bash,

    :~$ echo -i.$$ -i.27383
    you see what's passed on to perl, and see that there is a literal numner ( 27383 here ) passed in ...

    I think this was covered above, but i can't be sure.

    HTH

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;