Interpolation problem

P

Pal Hulden

Say I have this case:

----
#!/usr/bin/perl -w
$a = "(I leave now";
$_ = "Goodbye (I leave now)";
s/$a//;
print "$_\n";
----

I would like to expect

Goodbye )

as an answer. This does obviously not work (get unmatching parenthesis
error) due to more interpolation that I wanted. I have and have read
Wall/Christiansen/Schwarz' book 'Programming Perl' but I cannot find
the solution to the problem in there.

What is the correct syntax?

Please help.

Sincerely
Pål Huldén
Gothenburg
Sweden
 
K

Kien Ha

Pal said:
Say I have this case:

----
#!/usr/bin/perl -w
$a = "(I leave now";
$_ = "Goodbye (I leave now)";
s/$a//;
print "$_\n";
----

I would like to expect

Goodbye )

as an answer. This does obviously not work (get unmatching parenthesis
error) due to more interpolation that I wanted. I have and have read
Wall/Christiansen/Schwarz' book 'Programming Perl' but I cannot find
the solution to the problem in there.

What is the correct syntax?

perldoc -f quotemeta

$a = quotemeta $a;
s/$a//;

or
s/\Q$a//;
 
G

Gunnar Hjalmarsson

Pal said:
Say I have this case:

----
#!/usr/bin/perl -w
$a = "(I leave now";
$_ = "Goodbye (I leave now)";
s/$a//;
print "$_\n";
----

I would like to expect

Goodbye )

as an answer. This does obviously not work (get unmatching
parenthesis error) due to more interpolation that I wanted. I have
and have read Wall/Christiansen/Schwarz' book 'Programming Perl'
but I cannot find the solution to the problem in there

What is the correct syntax?

s/\Q$a//;
------^^

Check out perldoc perlre.
Gothenburg Sweden

Really? Me too. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top