[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

perl: problem with s/// and variable interpolation



Package: perl
Version: 5.004.04-3

I wrote a sample script whose behaviour seems strange to me.  At least
I can't find in the doc why it behaves so, nor what I should write to
get the expected result.

==== The sample script
#!/usr/bin/perl

$TRANSLATION = '\1;$2';
$str = "ab";
$str =~ s{(.)(.)}{$TRANSLATION};
print $str . "\n";

$str = "ab";
$str =~ s{(.)(.)}{\1;$2};
print $str . "\n";
==== Its output
\1;$2
a;b
==== End of example

What I'd expect is that the 2 portions of this example would be
equivalent (I'd like to have $TRANSLATION provided from command-line).

Unfortunately, it seems that the variable expansion only occurs for
depth 1.  Though I finally found a paragraph that seems somewhat
relevant to this, I'm not satisfied with this behaves and I'd be
grateful to anyone who has a solution/workaround for this.

It seems a similar phenomenon occurs in the left-part of s///: a
variable contaning eg. "$1" will have problems as well, though in this
case "\1" will work:

====
$DUMMY = '$1';

$str = "aba";
$str =~ s{(.)(.)($DUMMY)}{$1;$2};
print $str . "\n";

$str = "aba";
$str =~ s{(.)(.)($1)}{$1;$2};
print $str . "\n";
====
aba
a;b
====

Regards,
-- 
Yann Dirson  <ydirson@a2points.com>      | Stop making M$-Bill richer & richer,
alt-email:     <dirson@univ-mlv.fr>      |     support Debian GNU/Linux:
debian-email:   <dirson@debian.org>      |         more powerful, more stable !
http://www.a2points.com/homepage/3475232 | Check <http://www.debian.org/>


--
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org


Reply to: