in reply to How to handle passed data in subroutines

Try my first piece of demo. It prints "abc", not "ABC", because the return from uc() is simply discarded, and the value of $str is not changed at all. That's exactly what was missed from your understandng.

use strict; my $str = "abc"; uc($str); print $str;

The second demo gives you "ABC", not "abc", because we assigned the result returned from uc(), which is "ABC", back to $str:

use strict; my $str = "abc"; $str = uc($str); print $str;

BTW, you normally should pass array ref, not array, to your subs, for performance reason. Also, if you pass multiple arrays, or array mixed with other stuffs, you can easily lose track of the boundary of your array.

Replies are listed 'Best First'.
Re: Re: How to handle passed data in subroutines
by tilly (Archbishop) on Nov 20, 2003 at 05:30 UTC
    BTW, you normally should pass array ref, not array, to your subs, for performance reason.

    I consider this misguided advice.

    I think that people normally should pass whatever is clearest, and consider performance a distinctly secondary concern. This chapter is timeless and applicable across languages.

    Sure, this isn't always true. Occasionally you really do need to squeeze performance and straightforward good architecture choices, a bit of tuning bottlenecks, etc won't get you there. But in those problems, Perl is probably the wrong language to use in the first place.

    OTOH I agree with you when you say, Also, if you pass multiple arrays, or array mixed with other stuffs, you can easily lose track of the boundary of your array. However whether I think that this means that you should pass things into functions by reference depends on what the function does.