parametrical substitution in perl

A

ari

I want to write a subroutine which performs string substitution
parametrically, as follows:

sub doit()
{
my ($in,$before,$after)=@_;
my $out;
$out = $in;
$out =~ s/$before/$after/e;
return $out;
}

In general this works fine, but if I try and use captured buffers, it
doesn't work. For example, I want to do the following:

s/^(...)/$1 /

i.e. insert a blank after the first three chars.

if I do:

&doit("ABCDEF","^(...)",'$1 ');

I get back "$1 DEF"

Does anyone know how to make this work???
 
G

Gunnar Hjalmarsson

ari said:
I want to write a subroutine which performs string substitution
parametrically, as follows:

sub doit() ----------^^
Why?

{
my ($in,$before,$after)=@_;
my $out;
$out = $in;
$out =~ s/$before/$after/e;
return $out;
}

In general this works fine, but if I try and use captured buffers, it
doesn't work. For example, I want to do the following:

s/^(...)/$1 /

i.e. insert a blank after the first three chars.

if I do:

&doit("ABCDEF","^(...)",'$1 '); --^
Why?

I get back "$1 DEF"

Does anyone know how to make this work???

sub doit {
my ($in,$before,$after)=@_;
my $out = $in;
$out =~ s/$before/$after/ee;
return $out;
}

doit('ABCDEF', '^(...)', '"$1 "');
 
P

Paul Lalli

ari said:
I want to write a subroutine which performs string substitution
parametrically, as follows:

sub doit()
^^
Here, you're telling Perl that no parameter will be passed to this
subroutine.
{
my ($in,$before,$after)=@_;

.... yet, here, you're expecting three parameters to be passed to this
subroutine. This should be an error.

Except...
my $out;
$out = $in;
$out =~ s/$before/$after/e;
return $out;
}

In general this works fine, but if I try and use captured buffers, it
doesn't work. For example, I want to do the following:

s/^(...)/$1 /

i.e. insert a blank after the first three chars.

if I do:

&doit("ABCDEF","^(...)",'$1 ');
^^^
Here, you're telling Perl to ignore what you previously told it about
the subroutine. Why are you doing this?
I get back "$1 DEF"

Does anyone know how to make this work???

This topic came up in this group about a month ago:
http://groups.google.com/group/comp..._frm/thread/2a47b0f6f4ccbe2e/86a1fff0a9196208
The end result was that you need a double-eval on the s/// (ie, add an
additional /e modifier), and you need the replacement argument to be
whatever you want inside of double quotes.

doit("ABCDEF, "^(...)", q{"$1 "});
....
$out =~ s/$before/$after/ee;

See also: perldoc -q expand
(but note that the last time this came up, there was some controversy
as to the "goodness" of that FAQ answer).

Paul Lalli
 
A

ari

Paul -

You are right on both counts - my bad coding (the "()" after doit was a
mistake - although perl didn't seem to mind very much....

More importantly, your solution works, so much thanks!

Ari
 

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,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top