s/(.)/($1)/g vs s/(.)/$to/eg where $to='($1)'

J

jilerner

I'm looking for the solution with s/from/to/ when the to-part
comes from ARGV and references the $1. I tried /e and /ee
but still can't get this right.

Here's simlpified example. I need to get equivalent
of s/(.)/($1)/g , the difference being that the (the ($1))
comes from the variable: $to='($1)';

$_="abc"; s/(.)/($1)/g; # this does what I want -> "(a)(b)(c)"
$to='($1)';
$_="abc"; s/(.)/$to/g; # does not do what I want, I want result to be
"(a)(b)(c)"
$_="abc"; s/(.)/$to/ge; # does not do what I want
$_="abc"; s/(.)/$to/gee; # does not do what I want

Yakov
 
S

Steven Kuo

On 12 Apr 2005 (e-mail address removed) wrote:

(snipped)
I need to get equivalent
of s/(.)/($1)/g , the difference being that the (the ($1))
comes from the variable: $to='($1)';

$_="abc"; s/(.)/($1)/g; # this does what I want -> "(a)(b)(c)"
$to='($1)';
$_="abc"; s/(.)/$to/g; # does not do what I want, I want result to be
"(a)(b)(c)"
$_="abc"; s/(.)/$to/ge; # does not do what I want
$_="abc"; s/(.)/$to/gee; # does not do what I want




Here's one way:

$_ = "abc";
my $to = '"($1)"';
s/(.)/$to/eeg;
print;
 
T

Tad McClellan

Thanks a lot for the link and the answers therein.

Can you explain what are shortcomings
of the following solution that you mention ?:

my $search = 'foo(.*?)bar';
my $replace = 'wibble$1wobble';

s/$search/qq!"$replace"!/eeg;
die $@ if $@;


* Using eval() on data that comes from outside your program will
seriously damage your security

* Do not use it unless the template comes from
o Within your program
o A file considered part of your program source from the
point of view of security
o A user who has shell access anyhow (not in SUID scripts)

* You have to be really sure the above will always be true

my $evil_data='Ha ha ha... @{[ system "rm -rf /" ]}';
 
N

nobull

Thanks a lot for the link and the answers therein.

Can you explain what are shortcomings
of the following solution that you mention ?:

my $search = 'foo(.*?)bar';
my $replace = 'wibble$1wobble';

s/$search/qq!"$replace"!/eeg;
die $@ if $@;

The words I spoke to this slide were something along the lines:

It has calls eval() inside a loop (s///g is a looping construct) but
tests $@ outside the loop so will ignore errors from anything but the
last iteration.

It uses a single character quoting character (the double quote
character in this case) so will be confused by that character in
$replace.

The solution on the next slide correctly tests $@ inside the loop.

It uses a multi-character delimiter which is much less likely to
crop-up by chance.

Both solutions, of course, suffer from the usual hazards of using
eval() as described on the slide after.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top