Hi Pierre,
my @tmp = ( $var1, $var2, $var3 );
@tmp = map modify_variable, @tmp;

which is better

Conway (in Perl Best Practices) prefers the block form of map, since in his opinion, it's more readable. So you could rewrite it as:

my @tmp = ( $var1, $var2, $var3 );
@tmp = map { modify_variable } @tmp


I'm guessing that the code within modify_variable uses $_ under the hood? That may end up biting you later on, if you use modify_variable in different places. If $_ is in use by other code or gets changed on you unexpectedly, you may have some difficult debugging ahead of you. The following may prove clearer when reading it 6 months from now:

map { modify_variable($_) } = \($var1, $var2, $var3);


I personally like the above syntax - others may not. A foreach gets you an alias to each element, so what about the following? I've provided a modify_var sub as an example.

$var1 = 1;
$var2 = 2;
$var3 = 3;

foreach my $v ($var1, $var2, $var3) {
   modify_var(\$v);
}

sub modify_var {
   my $scalar_ref = shift || die "blaaaaa";
   ${$scalar_ref} += 5;
}

$, = "\n";   ## print a newline between each argument to print
print ($var1, $var2, $var3);

-m


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to