Matthew and Rob, thank you for your replies.

> - It's unclear whether you have a fixed set of variables to process.
> Is
>   the list always the same?

Yes, the list is always the same.

> - Why are you using references? Are you sure you need to?
> 
> - modify_variable() doesn't appear to modify anything, otherwise why
> are
>   you assigning its return value to the scalar passed as a parameter?
> It
>   seems to be just a function.

Modify_variable modifies its input variable.

Please correct me if I am wrong.
My understanding is that:
1) if I do:
        my @array = ($a, $b, $c);
        for (@array) { $_ = modify_variable($_)}
I am going to modify $array[0], $array[1] and $array[2], and NOT $a, $b,
$c.

2) if I do:
        for ($a, $b, $c) {$_ = modify_variable($_)}
I am going to modify $a, $b, $c, which is good, but if $a, $b, $c are
big I am going to be passing around lots of data.

So I think that using 2) with references on variables would be the ideal
solution.

> >From what I can see, you need no more than:
> 
>   $_ = function($_) foreach ($var1, $var2, $var3);
> 
> Will this do? Or is there more to the problem than you've explained?

Yes, that works in my case. I just thought that there might be a more
"elegant" way of doing it.

> 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 like this syntax too. The only difference in my case is that
modify_variable works with the content of the variable, not with a
reference (and I can't modify it).
So I will have to write it this way:
 
        map { modify_variable(${$_}) } = \($var1, $var2, $var3);

Thank you for your advices.



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


Reply via email to