All of the code provided in reply has been workable, but I notice that no one has mentioned a couple of errors in your code to you. While good solutions have been given to solve your current dilemma, I'd like to point the errors out to you so they don't bite you at a later date.

In your code, you attempt to test with $_ = "do_run". This is actually assigning 'do_run' to the variable. The string test operator is eq, so you want if $_ eq 'do_run'. (For numeric comparison, use ==). Also, you do not call a subroutine in perl -- just use the subroutine name.

When 'if' begins the line and is followed by a comparison, the comparison requires parentheses. Also, the block to be run on true needs to have brackets. Both of these can be eliminated in the switched around version (see below) due to precedence and syntax.

A final note (this isn't a bug, just a style issue) ... you are using double-quotes, which does variable interpolation within the string. Since you are not interpolating variables, single quotes will suffice. So the way a string comparison might look is:
if ($_ eq 'do_run') { do_run() }
The alternative style ("switched around" as I called it above) would appear as:
do_run() if $_ eq 'do_run';

-HZ

In reply to Re: switch statement for subroutines? by HyperZonk
in thread switch statement for subroutines? by zupereva

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.