Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: switch statement for subroutines?

by HyperZonk (Friar)
on Jul 28, 2001 at 04:19 UTC ( [id://100498]=note: print w/replies, xml ) Need Help??


in reply to switch statement for subroutines?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://100498]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-16 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found