Following the How to make an object method read named parameters? discussion on how to read named parameter hashes, I've been thinking about the rather non-intuitive nature of the various parameter processing techniques.

In essence, all of them are more-or-less workarounds for a lack of syntactic support in Perl itself for named parameter hashes.

Worse - they are all (with the exception of raw my %args = @_) very slow.

So, for consideration, here is a proposal for a module that uses source filters to add syntactic support for hash parameters (the code for this has been written and has tested to work all the way back to Perl 5.005_04):

use Sub::Parms; # Case insensitive function parms sub my_function { FunctionParms : %args; my ($a_name, $another_name) = @args{'a_name', 'another_name'}; # ... } # Case sensitive function parms sub my_other_function { CSFunctionParms : %args; my ($a_name, $another_name) = @args{'a_name', 'another_name'}; # ... } # Case insensitive method parms sub my_method { MethodParms : $self, %args; my ($a_name, $another_name) = @args{'a_name', 'another_name'}; # ... } # Case sensitive parms sub my_other_method { CSMethodParms : $self, %args; my ($a_name, $another_name) = @args{'a_name', 'another_name'}; # ... }

The difference the 'CSxxxx' versions handle is that the default mode is case insensitive processing of parameter names (they are all folded to lowercase), but the 'CSxxxx' versions are not folded to lowercase (and hence are Case-Sensitive).

The method declarations also handle the declaration of the class invokation parameter for the OO inclined

The thought is that the declarations could be extended in the future with more parameters to handle validation, defaults and variable binding options.


In reply to RFC named parameter syntactic sugar by snowhare

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.