Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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:

Complex interfaces

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).

sub set_choice { my ($self, $id, $name, $sub_or_id, $return_sub) = @_; $id !~ /^\d+$/ && die "ID must be a valid positive number"; $name || die "Name must be specified"; $self->{$id}{_id} = $id; $self->{$id}{_name} = $name; $self->{$id}{_sub_or_id} = $sub_or_id; $self->{$id}{_return_sub} = $return_sub; }

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:

my @entries = ("Entry #1","Entry #2","Entry #3","Quit"); my $count=0; for (@entries) { $menu->set_choice($count++,$_); }

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 reply to Re: Designing a package object by PhiRatE
in thread Designing a package object by fuzzyping

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found