Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have run across a situation where I need a Switch statement. After reading up on this online, it is my understanding that there really is no native Switch statement for PERL, and for later versions you should use the given/when construct (I'm using PERL v5.18.1 for Windows).

The problem is I can't seem to get the syntax right, even when using a dumbed down example I got from the web. Here's the code I'm trying to run:

use strict; use warnings; sub test2 { my $var = 2; my $i; given ($var){ when(1) { $i = "One"; } when(2) { $i = "Two"; } when(3) { $i = "Three"; } default { $i = "Other"; } } print "$var is $i"; } test2();

That gives me the following errors:

syntax error at D:/Eclipse_std_kepler_ws/StudyBuildReport/TestGiven.pl + line 9, near ") {" Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 9. Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 10. Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 11.

If I can't get this to work, I'll be stuck using an if/elsif construct, illustrated below (this example works fine):

use strict; use warnings; sub test1 { my $var1 = 2; my $b; if ($var1 == 1) { $b = "One"; } elsif ($var1 == 2) { $b = "Two"; } elsif ($var1 == 3) { $b = "Three"; } print "$var1 is $b"; } test1();

Any advice you can give would be deeply appreciated. Thanks.


In reply to Given When Syntax by Deep_Plaid

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 about the Monastery: (3)
As of 2024-04-24 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found