Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I started using Switch, then I read in CPAN "do not use if you can use given/when". Then when I used given/when, I ended up with warnings plus advice not to use given/when. Hmmmmmm. As an occasional perl user, that is a little frustrating rewriting the same section of code 3x to do exactly the same thing. I just need something that has case type functionality.

I can certainly understand your frustration. This was a very sad affair for the Perl community. Though adding smart match into Perl was premature, and had to be backed out, at least P5P learned a valuable lesson.

Not wanting to gloat (no, really) but this didn't affect me, at all, because I've never been a fan of switch. Stronger, I've almost never used Switch in over 20 years of coding in C++ and Perl and always queried its use during code reviews. Though it's a bit extreme to call Switch a code smell, cleaner alternatives, such as lookup-tables (hashes in Perl) and polymorphism (in OO languages, such as C++ and Java) should be preferred.

From Perl Best Practices I suggest you take a look at the Control Structures chapter, especially:

  • 6.16 Value Switches - Use table look-up in preference to cascaded equality tests (item 78)
  • 6.17 Tabular Ternaries - When producing a value, use tabular ternaries (item 79)

Though my advice in Perl is usually "just use a hash", as a last resort you could replace your switch with an if-elsif-elsif-else construct. See also:


In reply to Re^2: Yet another config file editing programme : Tell me how to make it better ! by eyepopslikeamosquito
in thread Yet another config file editing programme : Tell me how to make it better ! by dazz

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 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? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2022-08-11 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?