in reply to Substituting without modifying

If I understand your question... You want to perform a substitution on the value in $foo, without changing the value of $foo or copying the value to another variable first?

I think that you will be able to meet any two of those three requirements. :)

Here's something to consider, although it requires $foo to be a package variable rather than a lexical, and still involves copying the value:

$foo = "bar baz"; { (local $foo = $foo) =~ s/bar/quux/; # do something with the modified $foo } # now the original $foo is back again
BTW, there is nothing wrong with your use of my inside the parentheses.

Replies are listed 'Best First'.
Re: Re: Substituting without modifying
by sierrathedog04 (Hermit) on May 20, 2001 at 22:51 UTC
    Your method works great using lexical variables as well, because the perl interpreter correctly interpolates the temporary value of $foo when that temporary value is in scope.
    use strict; my $foo = "bar baz"; { (my $foo = $foo) =~ s/bar/quux/; # do something with the modified $foo $foo = "changed it"; print "The temporary value of foo is $foo.\n"; } # now the original $foo is back again print "The original value of foo is $foo.\n";
    Running under ActiveState 5.6.1 and Windows ME, the above code first returns the temporary value of "changed it" and then the original value of "bar baz".