literal substitution

D

Dave Walden

Hi,

Can someone tell me how I can obtain functionality like
$str =~ s/$a/$b/g;
where $a and $b contain strings and where instance of the the literal
string contained in $a are replaced by the literal string in $b
*without* regular expression interpretation. In other words, in the
following
my $str = 'moo \foo.* zoo';
my $a = ' \foo.*';
my $b = ' $';
$str =~ s/$a/$b/g;
I would like the value in $str to end up being 'moo $ zoo' while
allowing me to avoid having to escape in all the regex control
characters in the arguments to s///. Is there some qualifier I can use
to turn off regular expression interpretation in the arguments to s///
or some other way I can easily accomplish this without coding a new
subtroutine which takes the three arguments $str, $a, and $b and does
what I want?

Thanks, Dave
 
K

kens

Dave said:
Hi,

Can someone tell me how I can obtain functionality like
$str =~ s/$a/$b/g;
where $a and $b contain strings and where instance of the the literal
string contained in $a are replaced by the literal string in $b
*without* regular expression interpretation. In other words, in the
following
my $str = 'moo \foo.* zoo';
my $a = ' \foo.*';
my $b = ' $';
$str =~ s/$a/$b/g;
I would like the value in $str to end up being 'moo $ zoo' while
allowing me to avoid having to escape in all the regex control
characters in the arguments to s///. Is there some qualifier I can use
to turn off regular expression interpretation in the arguments to s///
or some other way I can easily accomplish this without coding a new
subtroutine which takes the three arguments $str, $a, and $b and does
what I want?

Thanks, Dave

You may want to look at the quotemeta function.

perldoc -f quotemeta

HTH, Ken
 
J

Jürgen Exner

Dave said:
Hi,

Can someone tell me how I can obtain functionality like
$str =~ s/$a/$b/g;
where $a and $b contain strings and where instance of the the literal
string contained in $a are replaced by the literal string in $b
*without* regular expression interpretation.

Sometimes I wonder why people are so obsessed with REs. Yes, they are great
and powerful and you you can do a lot with them.
However, why bend over backwards to make them fit a scenario they were not
meant for when there are much easier and straightforward solutions:
- use index() to find the location of string $a in $str
- use substr() to replace the text

jue
 
D

Dave Walden

Jürgen Exner said:
- use index() to find the location of string $a in $str
- use substr() to replace the text

Thanks. That's the memory jog I needed (although not as ideal as
having an additional qualifier to s/// that turned off regex control
character interpretation).
 
B

Brian McCauley

Dave said:
Thanks. That's the memory jog I needed (although not as ideal as
having an additional qualifier to s/// that turned off regex control
character interpretation).

Did you miss kens' reply?

s/\Q$a/$b/;
 
T

Ted Zlatanov

Sometimes I wonder why people are so obsessed with REs. Yes, they are great
and powerful and you you can do a lot with them.
However, why bend over backwards to make them fit a scenario they were not
meant for when there are much easier and straightforward solutions:
- use index() to find the location of string $a in $str
- use substr() to replace the text

The first part of the problem, finding a constant string, is
definitely better with index(), but I would argue REs are exactly
right for this task as a whole. You'd have to write at least 5 lines
of code vs. one simple line (quotemeta() makes it two lines); not to
mention the complexity and potential for bugs are higher. Yes, you
can jam index()/substr() into one or two lines, I'm assuming sane
maintainable code here.

If it was just one substitution, maybe it's a tie, but with the g flag
you have to loop too. Unless you were optimizing, why would you do
this to yourself and to whoever has to maintain your code later?

Ted
 
P

Paul Lalli

Ted said:
The first part of the problem, finding a constant string, is
definitely better with index(), but I would argue REs are exactly
right for this task as a whole. You'd have to write at least 5 lines
of code vs. one simple line (quotemeta() makes it two lines);

The \Q..\E representation of quotemeta() keeps it one line.

s/\Q$a\E/$b;

Paul Lalli
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top