Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

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

I haven't missed smartmatch much, just about the only thing I ever used it for was its distributive property, i.e. $foo ~~ ['a','b','c'], which can be replaced by Quantum::Superpositions or Perl6::Junction. I like the idea of being more specific with your conditions, e.g. $a eq $b, $a == $b, $a->($b), etc. - I find it more easily readable, which is better for later maintenance too. On the other hand, I also think implicit matching against $_ is a good idea (shorter code can be considered more readable too), as long as it's still obvious what the condition is.

I have missed the built-in "switch" (given/when) quite a bit, especially when as a statement modifier as well as given's ability to return the last value evaluated - i.e. my $x = do { given ($in) { "abc" when !defined; "def" when /d/; ... } };

I think the problem is that a truly perlish "switch" needs some kind of "smart matching" to be successful. And the problem with ~~ is not just in the implementation, but also that it allows for so much flexibility that people have different ideas on how it's "supposed" to interpret its arguments. Your post is one example, and for completeness, here's my view: a basic "switch" should be able to automatically apply defined, eq, ==, and =~ to the cases, and some way to specify multiple conditions per case (in the case of smartmatch, that would be its distributive property).

So, a while back I did some simple evaluations of some of the "switch" alternatives out there. Here are my notes from that evaluation (no particular order):

  • Switch
    • uses source filters, buggy, has been removed from core
  • Switch::Plain
    • fairly simple and straightforward
    • separate sswitch / nswitch statements
    • requires Perl >= v5.14
  • Switcheroo
    • more powerful than Switch::Plain
    • quite a few dependencies, including Parse::Keyword which is marked as "DEPRECATED"
    • requires Perl >= v5.14 (requires Perl keyword API)
  • Switch::Perlish
    • uses native perl syntax
    • includes its own "smart matching"
    • has been failing tests since since Perl 5.13.6

I personally like Switch::Plain and Switcheroo, I'm a little turned off by the simplicity of the former and the dependencies of the latter (matter of taste, I know), and both require at least Perl v5.14, which isn't nice for backwards-compatible code.

So, I find myself still writing if-elsif chains and spelling all my conditions out, while sometimes wishing for the core given/when back, at least with the simple cases I use it for...


In reply to Re: Bring back the smartmatch operator (but with sane semantics this time)! by Anonymous Monk
in thread Bring back the smartmatch operator (but with sane semantics this time)! by smls

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 scrutinizing the Monastery: (2)
As of 2024-04-20 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found