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.


---
demerphq

    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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.