Typically this type of issue arises with programers and programs from other languages like C. In C you can return at most a single item from a function so such "pass a reference and have the sub change the referenced item" strategies are necessary when you need to return multiple items. Perl doesnt have these restrictions so this type of stuff is generally frowned apon and considered to be unperlish. And not only that, but perl has a funky (but poorly documented and explained IMO) mechanism for handling it when you do need those type of semantics.
First off in perl if we want to return several things:
sub foo { return ('we','do','so') }
Second, if you want to an argument via reference semantics dont pass in a reference, instead let perl do it for you
sub foo { $_[0]='foo' } my $x='bar'; foo($x); print $x; #prints 'foo'
@_ is special in that it is an array of aliases to the original argument list. Altering members of the array will change the original values. But once the value is pop'ed or shift'ed off the array the aliasing is blown. For instance
sub foo { my $x=shift; $x='foo'}
won't work the same as the earlier version that exploits aliasing.
Having said all of this, if you are aiming at converting C code into perl code then think about returning lists, and not modifying your subroutines arguments. I think youll find in the standard documentation some stuff to aid programmers from other backgrounds coming up to speed with perlish ways to do things.
Also, its worth considering that doSomething() isnt a popular style in the perl community for a variety of reasons. Generally most would lean towards do_something() instead. I certainly would.
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
In reply to Re: A question of style
by demerphq
in thread A question of style
by Scarborough
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |