really quick programming question!!! Perl and reg ex's

evident

Lifer
Apr 5, 2005
12,150
773
126
Hi all,

it's late and my brain isnt functioning right, if any of you guys can help me i would greatly appreciate it.

i am basically just trying to to a find and replace in perl, and then outputting the result to a file.

i have got $temp="foo";
i have the variable $_ = "Bunch of text";

i want to replace "text" w/ the string stored in $temp.

i've tried using a regular expression, s/text/$temp
but in my output file i get Bunch of $temp

but i would like to have Bunch of foo

how would i go about doing this?

 

Aluvus

Platinum Member
Apr 27, 2006
2,913
1
0
Make sure you're putting double quotes ("") around the regex, rather than single quotes ('').

Disclaimer: I don't actually do Perl, but this might help anyway.
 

Skeeedunt

Platinum Member
Oct 7, 2005
2,777
3
76
$text = "foo";
$_ = "Wrong text forum text";

$_ =~ s/text/$text/g;

print OUTFILE $_;

OUTFILE should have "Wrong foo forum foo". If you only want the first instance replaced, take out the trailing g in the regexp.

Oops fixed.