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

Re: perl: problem with s/// and variable interpolation



-----BEGIN PGP SIGNED MESSAGE-----

Yann Dirson, in an immanent manifestation of deity, wrote:
>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.

Two things:
1) If you want stuff on the right hand side to eval-ed (double
   substitute variables), you need to do it yourself.
   Note that with:
     $TRANSLATION = '\$1;\$2'; $str = "ab";
   both
     $str =~ s{(.)(.)}{$TRANSLATION}ee;
   and
     eval {$str =~ s{(.)(.)}{$TRANSLATION}; };
   are faster than:
     eval "\$str =~ s{(.)(.)}{$TRANSLATION}";

2) Using \1 etc. only works on the left side of a substitute.  It will
   frequently work on the right side but this is deprecated behaviour
   and shouldn't be counted on.
   $1 etc. are only filled after the regular expression is done and
   therefore using it on the left side of a substitute will have
   unpredictable results and won't do what you expect.

Darren
P.S. Sorry I didn't answer this sooner.  I've been a touch busy.
- -- 
<torin@daft.com> <http://www.daft.com/~torin> <torin@debian.org> <torin@io.com>
Darren Stalder/2608 Second Ave, @282/Seattle, WA 98121-1212/USA/+1-800-921-4996
@ Sysadmin, webweaver, postmaster for hire.  C/Perl/CGI programmer and tutor. @
@		     Make a little hot-tub in your soul.		      @

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3a
Charset: noconv
Comment: Processed by Mailcrypt 3.4, an Emacs/PGP interface

iQCVAwUBNU1mbo4wrq++1Ls5AQGEUQP/RGbwwQp3QzjTuNrM03crQT2G7OcuYXB5
h8bsoNaexdRPOoWj3tu/SsQRJpMJYS6bYVUvtbFx3iYw4qPygqDyfSKn2n8NPMMM
aDfiCDiEWIrIxPTNp3SB85+sa31s81437UVBnX24s7WlluXyM0osUq51REaNmv47
FCNIgGH4HEM=
=KVPB
-----END PGP SIGNATURE-----


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


Reply to: