Thanks to the way that perl is untyped (to some extent), it's quite possible to have functions that can take any type of parameters, and have the function adapt to several possible input methods.
I'm thinking along the same lines for a data entry function for a module I'm writing. Each point can contain 2 required data elements, an optional name parameter, and an optional hash containing options for that point. Internally to the module, I'll be storing all this as a hash of hashes, but from an external standpoint, I can see many ways of allowing the data to be entered. These include:
-
Array of arrays, where each internal array are the two elements, name, and option
-
2 + 2 optional array refs, where the first two arrays are the list of the require elements, the 3rd as the names, and the 4th as the options.
-
Array of hashes, where the hash should containing key fields (looked for by various names) indicating the two required elements, and the optional name and options.
-
Hash of hashes, where the key of the hash can be the name, and the hashes otherwise look as with the above.
In each of these cases, it's easy to see how one might have gotten the original data into one of the formats above, so I'm simply trying to make it as easy to convert from what a user might have to my internal storage. And, save for one case, it's easy enough to determine what format is used based on using
ref and other inspection techniques. (The one case would be between the array of arrays, and the up-to-4 arrays; if inspection of the @_ array (after removing other arguments not related to the data) revealed more than 4 elements, it would be assumed that the former case is what is indended by the user.)
Is such complicated distinguishing of a mutable argument a good thing for development? It's certainly not a problem to replace this mutable call with several functions that have more specific ways of accepting data, but that would seem to complicate the API of the module, requiring the user to remember exactly what is to be passed to each function. A function call with such a mutable argument begins to move the module usage to a state of "do what I mean, not what I say" in terms of user interaction, requiring them to remember less about the specifics of the API and focusing them on their own programming more.
Are there any options or ideas in regards to this?
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.