Just another Perl shrine | |
PerlMonks |
RFC: New style for argument check in subsby LanX (Saint) |
on Sep 05, 2012 at 10:16 UTC ( [id://991802]=perlmeditation: 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 ...
... 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
*** Unpack Named Later
*** Unpack Named On The Fly
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.
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.
Back to
Meditations
|
|