[Wellington-pm] The Perl Way: References & subroutines

Jacinta Richardson jarich at perltraining.com.au
Sun Feb 20 20:27:08 PST 2005


Kevin Dorne wrote:

> my %msg;
> fetch_next(\%msg);
> sub fetch_next {
>   my $msg = shift;
>   ...
> }

Be aware that this is action from a distance.  If I don't expect 
fetch_next to change my hash then I might be rudely upset when it does. 
  As a code maintainer, it's always nicer to see

     $msg = fetch_next();

than

     fetch_next(\%msg);

because then I can easily see that the contents of $msg is the result of 
calling fetch_next.  In the second case it's too easy to assume that 
fetch_next merely uses the contents of %msg without making changes.

I suggest trying to code with the principal of least surprise.  Which 
means going with your example (2).  There should be no appreciable 
efficiency difference between the two.

All the best,

     Jacinta
-- 
    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
  (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




More information about the Wellington-pm mailing list