Use a dispatch table instead:
my %actions = ( 1 => \&error_entry, 2 => \&warning_entry, ); my $action_sub = $actions{$value_to_check} || \&default_action; $action_sub->($args_for_this);
Your individual actions need to have matching call signatures (same number of arguments, same order) for this to work. If your subs already exist and don't have exactly the same signatures, you can either write wrapper subs or use anonymous subs in the actions table. Note that if you want to call methods, you'd simply put the method names in the action table and then use this to call the selected method:
$object->$method($args_for_this);
This makes it totally trivial to add or change the number of alternatives, and is a hash lookup, so no matter how many alternatives you have, the time to find and run any given one is exactly the same (handwaving hash collisions here).

In reply to Re^5: Why is my script not printing anything?and how to add a switch case in perl by pemungkah
in thread Why is my script not printing anything?and how to add a switch case in perl by iphone

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.