perl -pe substitution
am 18.01.2008 23:43:22 von Asim Suter
Case 1)
my $VAR = 'ABC@VERSION@DEF' ;
$VAR =~ s/\@VERSION\@/1.2.3.4/g ;
print "<$VAR>\n" ;
Prints as expected.
=========================
Case 2)
Lets say $input_file has a line containing ABC@VERSION@DEF
And I want to replace @VERSION@ by 1.2.3.4 So I do:
`perl -pe 's/\@VERSION\@/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 play but it's escaped - but
not sure any ideas ?
Thanks!
Re: perl -pe substitution
am 18.01.2008 23:57:11 von Abigail
_
Asim Suter (asuter@cisco.com) wrote on VCCLIII September MCMXCIII in
:
``
``
`` Case 1)
``
`` my $VAR = 'ABC@VERSION@DEF' ;
``
`` $VAR =~ s/\@VERSION\@/1.2.3.4/g ;
``
`` print "<$VAR>\n" ;
``
``
``
`` Prints as expected.
``
`` =========================
``
`` Case 2)
``
`` Lets say $input_file has a line containing ABC@VERSION@DEF
``
`` And I want to replace @VERSION@ by 1.2.3.4 So I do:
``
`` `perl -pe 's/\@VERSION\@/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 @ from the *OUTER* perl - that is, the
outer perl sees '@VERSION@', which for the inner perl is the array @VERSION
followed by @. So you would have to write something like:
`perl -pe 's/\\\@VERSION\\\@/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 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 !@ _?
exit print :
print and push @_ => shift and goto &{(caller (0)) [3]}}
split // => "KsvQtbuf fbsodpmu\ni flsI " xor & _
Re: perl -pe substitution
am 21.01.2008 05:48:42 von Joe Smith
Asim Suter wrote:
> `perl -pe 's/\@VERSION\@/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 play but it's escaped
It's not escaped to the shell nor to the second invocation of perl.
@VERSION = qw(aaa bbb);
$_ = `echo perl -pe 's/\@VERSION\@/1.2.3.4/g' -i $input_file`;
$_ = `echo perl -pe 's/\\@VERSION\\@/1.2.3.4/g' -i $input_file`;
$_ = `echo perl -pe 's/\\\@VERSION\\\@/1.2.3.4/g' -i $input_file`;
system qq{perl -pe 's/\\\@VERSION\\\@/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