|laziness, impatience, and hubris|
Re: Designing a package objectby PhiRatE (Monk)
|on Jul 25, 2002 at 05:47 UTC ( #185127=note: print w/replies, xml )||Need Help??|
Others have mentioned the die() error, and I believe you have already solved the HoH problem but I'll go into detail about a "theoretical" problem in addition:
The problem primarily is that your set_choices interface takes a complex datatype. This gives you very little room for in-module syntax checking. You can either reject the entire set of choices and leave the interface-programmer wondering where they went wrong, or not check it at all and end up with the interface-programmer scratching their head at what is presented to them on the screen.
Rather than taking a HoH as a single argument to the set_choices() function, you should instead repeatedly call an add_choice() function or equivalent to add choices. Yes, its a little more work, but the advantage is that you can do nice checks against the data as it comes in without having to traverse the complex type itself, you can also (but not in this instance) use perl prototyping to help the interface-programmer get their code right at compile time.
The most important benefit however is documentation simplicity. It is trivial to document the following sub, whereas documenting set_choices() above would require being very specific about the nature of each of the entries, the format required, plus the duplication of _id and the hash key (which is also $id).
This also means that the interface-programmer can store their menu entires in whatever form is convenient to them, without having to do the (possibly error-prone) translation to your format before submission, ie:
Which may be considerably more convenient for them, especially if the menu items are coming out of a database or part of a larger data structure.
Always try to keep the interfaces simple, its the point at which the interface-programmer and the module-programmers minds have to mesh, and thus the weakest point in the application.
In Section Seekers of Perl Wisdom