There is one thing you can do to make your program structure really neat, and that is to use a HoH (also could be AoA, AoH or whatever two-level sturcture that fits) to store your call back functions.

Give each of your question a unique identifier, could be a number or a string. Also give each of your choices an identifier, which is unique within the related question.

To demo, I just use string as identifier, and pick HoH as the structure. I can store all the callback functions in this way:

my $callback_functions = { #I am not suggesting this kind of 1,2,3,4 naming convention in you +r actual code, this is just for demo, it is never a good idea to hard + code the index as part of your identifier. "question1" => { "answer1" => \&func1, "answer2" => \&func2, "answer3" => \&func3, }, "question2" => { "answer1" => \&func4, "answer2" => \&func5, "answer3" => \&func6, }, "question3" => { "answer1" => \&func7, "answer2" => \&func8, "answer3" => \&func9, } };
But you better use some meaningful identifiers.

By doing this, you can actually avoid those tedious nested if-else, and make your logic much more straight and clear. Now the differences among questions and answers is detached from your logic, and is largely contained in that callback structure.

In reply to Re: simple command line program by pg
in thread simple command line program by primus

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.