Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
As a matter of maintainable style, it's often discouraged to allow your subroutines to modify the values of their parameters. But as with most things Perlish, this rule of thumb only applies where convenient. After all, chomp modifies its parameter list. So does chop.

I'd like to add that this style recommendation will probably be affected by your background (or the background of the senior people in your department) (as davido said, 'where convenient'). For those with a C background, it make perfect sense to modify the arguments, as they can only return one value from a function. So it's common practice to have the function return a success or failure flag, and modify what was passed in. (it's part of the whole issue if you're going to pass by value, or pass by reference), as if you pass by value, there's no chance to modify the values. PL/SQL has a similar concept called 'in/out' parameters which work like pass by reference.

There is, however, the expectation in almost all languages that you're not going to modify the references that were passed in, unless that was part of the plan, and it's well documented. The local style guide might have recommendations on function naming, so it's readily apparent to people that the function does modifications on its args. The following would be an example of bad code:

sub sum { my $list = shift; my $sum = 0; while (my $i = shift @$list) { $sum += $i; } return $sum; } my @values = qw(1 2 3 4 5 6 7); print sum(\@values), " ; '@values'\n";

You should always be suspicious when there's only one argument to the function, and it's an arrayref, not an array ... there might be some time savings by not pushing everything to the stack, but it introduces the possibility of someone messing with your values. Two or more arrayrefs isn't so much a sign to worry, as arrayrefs are the only way to pass more than one array to a function.

Depending on the audience, something such as the following might be considered acceptable:

sub add_two { my $arrayref = shift; $_ += 2 foreach @$arrayref; return 1; } my @values = qw( 1 2 3 4 5 6 7 ); print add_two(\@values), " ; '@values'\n";

Okay, that might be a bad example. You might do it when you wanted to make sure that the original wasn't left around, though. Such as:

sub sanitize_strings { my $arrayref = shift; s/[^a-zA-Z0-9_\-]/_/g foreach @$arrayref; return 1; } my @strings = qw( abc 123 a&21 j**!k ); print sanitize_strings(\@strings), " ; '@strings'\n";

In reply to Re^2: What are multiple $_[0] =~ operations doing? by jhourcle
in thread What are multiple $_[0] =~ operations doing? by Plotinus

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (4)
    As of 2021-01-25 00:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?