perl -pe substitution

Case 1)

my $VAR = 'ABC [at] VERSION [at] DEF' ;

$VAR =~ s/\ [at] VERSION\ [at] /1.2.3.4/g ;

print "<$VAR>\n" ;



Prints <ABC1.2.3.4DEF> as expected.

=========================

Case 2)

Lets say $input_file has a line containing ABC [at] VERSION [at] DEF

And I want to replace [at] VERSION [at] by 1.2.3.4 So I do:

`perl -pe 's/\ [at] VERSION\ [at] /1.2.3.4/g' -i $input_file` ;

Which chnages the content in $input_file to

ABC1.2.3.4VERSION1.2.3.4DEF

Why the difference in the two cases ?

What do I need to do to get bahavior like Case 1 in Case 2 ?

I'd suspect some special interpretation of [at] at play but it's escaped - but
not sure any ideas ?

Thanks!
Asim Suter [ Fr, 18 Januar 2008 23:43 ] [ ID #1911692 ]

Re: perl -pe substitution

_
Asim Suter (asuter [at] cisco.com) wrote on VCCLIII September MCMXCIII in
<URL:news:1200696203.76315 [at] sj-nntpcache-3.cisco.com>:
``
``
`` Case 1)
``
`` my $VAR = 'ABC [at] VERSION [at] DEF' ;
``
`` $VAR =~ s/\ [at] VERSION\ [at] /1.2.3.4/g ;
``
`` print "<$VAR>\n" ;
``
``
``
`` Prints <ABC1.2.3.4DEF> as expected.
``
`` =========================
``
`` Case 2)
``
`` Lets say $input_file has a line containing ABC [at] VERSION [at] DEF
``
`` And I want to replace [at] VERSION [at] by 1.2.3.4 So I do:
``
`` `perl -pe 's/\ [at] VERSION\ [at] /1.2.3.4/g' -i $input_file` ;

^
|

You have backticks here. Are you calling perl from withing perl?
If so, you're excaping the [at] from the *OUTER* perl - that is, the
outer perl sees ' [at] VERSION [at] ', which for the inner perl is the array [at] VERSION
followed by [at] . So you would have to write something like:

`perl -pe 's/\\\ [at] VERSION\\\ [at] /1.2.3.4/g' -i $input_file`;

``
`` Which chnages the content in $input_file to
``
`` ABC1.2.3.4VERSION1.2.3.4DEF
``
`` Why the difference in the two cases ?
``
`` What do I need to do to get bahavior like Case 1 in Case 2 ?
``
`` I'd suspect some special interpretation of [at] at play but it's escaped - but
`` not sure any ideas ?

You need to escape it for each level of possible interpolation. Which means,
twice in this case.



Abigail
--
sub _ {$_ = shift and y/b-yB-Y/a-yB-Y/ xor ! [at] _?
exit print :
print and push [at] _ => shift and goto &{(caller (0)) [3]}}
split // => "KsvQtbuf fbsodpmu\ni flsI " xor & _
Abigail [ Fr, 18 Januar 2008 23:57 ] [ ID #1911693 ]

Re: perl -pe substitution

Asim Suter wrote:

> `perl -pe 's/\ [at] VERSION\ [at] /1.2.3.4/g' -i $input_file` ;
>
> Which chnages the content in $input_file to
>
> ABC1.2.3.4VERSION1.2.3.4DEF
>
> Why the difference in the two cases ?
>
> What do I need to do to get bahavior like Case 1 in Case 2 ?
>
> I'd suspect some special interpretation of [at] at play but it's escaped

It's not escaped to the shell nor to the second invocation of perl.

[at] VERSION = qw(aaa bbb);
$_ = `echo perl -pe 's/\ [at] VERSION\ [at] /1.2.3.4/g' -i $input_file`;
$_ = `echo perl -pe 's/\\ [at] VERSION\\ [at] /1.2.3.4/g' -i $input_file`;
$_ = `echo perl -pe 's/\\\ [at] VERSION\\\ [at] /1.2.3.4/g' -i $input_file`;
system qq{perl -pe 's/\\\ [at] VERSION\\\ [at] /1.2.3.4/g' -i $input_file};


Take a look at 'perldoc backticks'. In particular, the sections for
What's wrong with using backticks in a void context?
How can I call backticks without shell processing?

-Joe
Joe Smith [ Mo, 21 Januar 2008 05:48 ] [ ID #1913438 ]
Perl » comp.lang.perl.misc » perl -pe substitution

Vorheriges Thema: FAQ 8.49 How do I add a directory to my include path (@INC) at runtime?
Nächstes Thema: FAQ 8.39 How do I set CPU limits?