Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (5)
    As of 2018-05-27 10:24 GMT
    Find Nodes?
      Voting Booth?