I'm currently debugging a subroutine that takes exactly 61 arguments. After spending a few days working out how it's called ("... so here argument 37 is null, but over here argument 37 is "Phooey", and argument 53 is null, except when ..."), I'm wondering why more people don't call subroutines with a single hash as the argument.

Is it dreadfully expensive, in terms of memory usage? It's more verbose, true, but it means that:

Am I missing something? I can see this would be pointless if there are only a few arguments to be passed, but it looks like it could improve my code legibility no end.

What do monks think? I know that hashes as arguments has come up a few times before (like here, for one), but no-one seems to have expressed whether it's a good idea, a bad idea, or JAWTDI.


For novices who aren't sure what I'm not about, try this:

&foo( bork => 'glorp', ningi => 'fertangg!', flarp => undef ); exit; sub foo(%) { my %args_of = @_; foreach ( keys(%args_of) ) { print "The argument '", $_, "' was set to: ", defined( $args_of{$_} ) ? $args_of{$_} : '(undefined)', "\n"; } 1; }

which outputs:

The argument 'bork' was set to: glorp
The argument 'ningi' was set to: fertangg!
The argument 'flarp' was set to: (undefined)

--

bowling trophy thieves, die!

2003-06-05 edit ybic: <readmore> tags, monasterialized link to related prior thread ( like so [id://120181] ) to prevent logout for monks with cookie not from "www".


In reply to passing subroutine args as a hash: why not? by Willard B. Trophy

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.