Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

perl -pe substitution

34 views
Skip to first unread message

Asim Suter

unread,
Jan 18, 2008, 3:24:21 PM1/18/08
to

Case 1)

my $VAR = 'ABC@VERSION@DEF' ;

$VAR =~ s/\@VERSION\@/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@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!


Dr.Ruud

unread,
Jan 20, 2008, 7:27:08 AM1/20/08
to
Asim Suter schreef:

> s/\@VERSION\@/1.2.3.4/g

Consider a "while ( my $p=index() ){ substr(,,,) }" approach.

--
Affijn, Ruud

"Gewoon is een tijger."

Joe Smith

unread,
Jan 20, 2008, 11:48:42 PM1/20/08
to
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

0 new messages