Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Passing a $1 within a scalar to a s/// doesn't work. Why?

Reply
Thread Tools

Passing a $1 within a scalar to a s/// doesn't work. Why?

 
 
Rob Campbell
Guest
Posts: n/a
 
      06-03-2005
Hi,

Still in the early stages of this Perl business but haven't been able to
find an answer to this question anywhere. Perhaps someone here can help.

I have a little function which takes as its inputs two regular expressions.
The first line of the function is:
my($simulate,$oldpart,$newpart,@rest) = @ARGV;

The function just uses these to perform a substitution:
$d =~ s/$oldpart/$newpart/;

The problem is that when I want to capture text in the first expression to
use it as a "$1" in the second, it don't work: the '$1' is interpretted
literally.

i.e.
if $oldpart contains 'binoise\w*(G1_[0-9]+)\.src', and and $newpart contains
'_binoise.src'; then I get, for example:
../binoise9G1_12.src -> ./_binoise.src

Not what I want, but fine, it works. However, if $newpart now contains
'$1_binoise.src' then I get:
../binoise9G1_12.src -> ./$1_binoise.src

Huh? Why is the $1 being interpreted literally. I've not done anything else
silly since if I hard-code my substitution so that it looks like this:
$d =~ s/$oldpart/$1_binoise/;
Then I get what I want:
../binoise9G1_12.src -> ./G1_12_binoise


I've tried escaping the $ in $newpart but that doesn't work either. Why does
Perl interpret my substitution command differently when I feed it a
variable compared to when I feed it the regexp directly? How do I get it to
play ball and allow a $1 to be interpretted correctly when it's passed to
my s/// command in a scalar.

Thanks!
Rob
--
remove "FERRET" to reply
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-03-2005
Rob Campbell wrote:
> How do I get it to play ball and allow a $1 to be interpretted
> correctly when it's passed to my s/// command in a scalar.


perldoc -q "expand variables"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-03-2005
Jim Gibson <> wrote:

> I am not quite sure why '$d =~ /$oldpart/$newpart/e;' doesn't work,



Because the replacement part is now code rather than a string.

If you instead said

$foo = $newpart;

you would expect a literal '$1' in $foo's value, as there is only
one round of evaluation and that is used to fetch the value from $newpart.

Same thing for the code above.


> but
> wrapping the replacement text in double quotes and forcing another
> round of evaluation works:



That would be like

$foo = eval $newpart;

were you _would_ expect variables in $newpart's value to be eval()uated.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      06-04-2005


Rob Campbell wrote:

[ My favourite question ]

See my lightning talk on about this question from YAPC::Europe::2004.

http://birmingham.pm.org/talks/faq/Hello.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing an array from FORTRAN to C then passing it within C andReturning it to FORTRAN deadpickle C Programming 1 11-07-2010 02:38 PM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
stop plus (+) symbol within scalar variable being interpolated in RE when followed by a number Tom Perl Misc 6 12-16-2003 12:33 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM
Re: Error: can't modify scalar reference Purl Gurl Perl 0 08-18-2003 07:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57