Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hi

I'm trying to extend Damians PBP advices for unpacking and then checking arguments using the ternary-operator ...

sub padded { my ($text, $arg_ref) = @_; # Set defaults... # If option given... Use option Else defau +lt my $cols = exists $arg_ref->{cols} ? $arg_ref->{cols} : $DEF_PAG +E_WIDTH; my $filler = exists $arg_ref->{filler} ? $arg_ref-> : $SPACE; # ... etc return $filler x $left . $text . $filler x $right; }

... by using the defined-or operator, which was new in 5.10 long after PBP was published.

It's more concise ... but actually I'm not sure how to handle named arguments╣ so which pattern do you think is better?

*** Unpack Named First

sub unpack_named_first { my ($pos1, $pos2 ,%arg) = @_; # unpack my ($name1, $name2) = @arg{'name1','name2'} $pos1 // die("Missing arg!"); # obligatory $pos2 //= 42; # default $name1 // die("Missing arg!"); # obligatory #$name2 # optional # ...etc }

*** Unpack Named Later

sub unpack_named_later { my ($pos1 ,$pos2 ,%arg) = @_; $pos1 // die("Missing arg!"); $pos2 //= 42; $arg{name1} // die("Missing arg!"); # if you need to unpack hash-args my ($name1,$name2) = @arg{qw/name1 name2/}; # ...etc }

*** Unpack Named On The Fly

sub unpack_named_on_the_fly { my ($pos1 ,$pos2 ,%arg) = @_; $pos1 // die("Missing arg!"); $pos2 //= 42; my $name1 = $arg{name1} // die("Missing arg!"); my $name2 = $arg{name2}; # ...etc }

My intentions are:

1. to have a concise pattern to replace the (alas) missing sub-signatures in perl

2. to follow more DRY practice

3. The patterns should be also usable in parts, i.e. further arg constraints could be extendable later, e.g. using ternaries

4. to have self-documenting code.

5. to be parsable for inspection and/or POD-creation and/or IDE-expansion.

a further extension could be to replace die() with a wrapper called missing(), which uses caller to pretty-print source-line and function-name.

$pos1 // die_miss(); $pos2 // warn_miss();
Personally I tend to the first option to unpack first, even if it's more work to refactor if someone decides later that he needs to unpack %arg which he used directly before, ie. $arg{name}

Cheers Rolf

╣) please don't be confused:

1. at my current project we are not following Damian's advice to use an anonymous hash for named args.

2. testing for existence instead for definedness is such a rare special case that it should be explicitely coded.


In reply to RFC: New style for argument check in subs by LanX

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 rifling through the Monastery: (9)
    As of 2014-12-25 03:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (159 votes), past polls