tags:

views:

109

answers:

2

Hi all,

I always find myself writing code like this:

my $var = $result[0];
my $var_changed = $var;
$var_changed =~ s/somepattern/somechange/g;

What would be a better way to do this? And by 'better' I mean: less typing while remaining understandable.

Thanks.

+14  A: 

This would do the same thing as the second and third lines;

(my $var_changed = $var) =~ s/somepattern/somechange/g;

How legible it is is your call.

Plutor
Legibility aside, it's very idiomatic and something you're likely to see in code written by experienced Perl developers.
Michael Carman
+1  A: 

Or even

(my $var_changed = my $var = $result[0]) =~ s/somepattern/somechange/g;

But that starts to bring into question why you need $var in the first place.

Chas. Owens