Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
While most of people gave you various (and very good) responses. I'd like to comment on this.

I really like this way it's done in Perl. Sure it takes a bit of time to adjust to it - but once you do that. It comes somehow naturally.

Also - once I discovered how hashes are great (before Perl I only used C on university, and some Basic variants like M$ VB and old Amiga AMOS on my own) I started almost just using them :)

They are so great because many of the modules use them, and at the end of the day, my jobs ends up in passing references from one module to other :)

In your case (actually in almost any case) I would probably use something like:

sub_call ({ first => $first, second => \@second, third => $third, }); # ... sub sub_call { my $hash_ref = shift; # $hash_ref->{first} holds original $first # $hash_ref->{second} holds reference to original @second # $hash_ref->{third} holds original $third # NOTICE - just like passing pointers in C. # When you modify the array that array_ref points to (to @second) # you are changing the original. Might not be what you want. # In that case, pass @second, not \@second to get a "local copy" }
Or other similar option: < EDIT: As sugested a little earlier >
sub_call ( first => $first, second => \@second, third => $third, ); sub sub_call { my %hash = @_; # $hash{first}, $hash{second}, $hash{third} hold values like above }

Good thing about this, is that when you write the sub, you don't need to set the local variable for each sent parameter. The order of parameters also isn't important, but caller needs to know exact names that you use through the sub. IMHO I just like this way better.

While you can use any of the above, first one (passing reference) should be better in case you have many variables (in this case tree, since we pass array_ref). I would suggest you actually use just it.
So you wouldn't come into a doubt which one did you used for particular sub. And be constant in it.
Maybe someone wouldnt agree with all this?

I also noticed that many CPAN modules use this for configuration type method calls.

PS. This way you predefine the names of the variables but not their placing in sub call :)

In reply to Re: Predefining sub Parameters by techcode
in thread Predefining sub Parameters by willyyam

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 meditating upon the Monastery: (8)
    As of 2014-08-22 13:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (158 votes), past polls