Welcome to the Monastery | |
PerlMonks |
Re: Thoughts on "one function, flexible arguments"?by davido (Cardinal) |
on Aug 29, 2019 at 07:15 UTC ( [id://11105221]=note: print w/replies, xml ) | Need Help?? |
While at the outset it would seem that this adheres to Postel's law ("Be liberal in what you accept, and conservative in what you send.", paraphrased.), it's a bit of a path toward madness. The more alternatives you allow, the more you are agreeing to support all those alternatives forever, for some values of forever. You are also adding complexity to your code, making param unpacking more difficult, particularly in special cases, and are painting yourself into a corner where the calling alternatives may prevent you from doing something with the args that makes perfect sense in a specific case, but differs from the myriad of ways all of the other methods in your library work. I suggest either requiring a hashref, or permitting either a hashref or key/value pairs. Don't go providing coercing of array-refs into hash-refs, or scalar refs, or code-refs (executed to produce the args)... unless you have a good reason for providing one of those, and in that case don't provide all the other ways. It's really an overzealous application of the perceived spirit of Postel's law, but isn't founded in the actual intent. Also if you do find yourself doing this often, consider putting the args unpacking into a subroutine or method that can be called for every sub with conforming arg handling. If you really need to provide significantly different ways to pass args, consider writing one version of the method, and then creating wrapper methods that provide the alternate calling styles:
There is a module, Params::Smart which will handle both positional and key/value args, but the time I used it I kind of wish I had just created two subs to call instead. Dave
In Section
Seekers of Perl Wisdom
|
|