Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Bring back the smartmatch operator (but with sane semantics this time)!

by Anonymous Monk
on Jun 10, 2014 at 20:52 UTC ( [id://1089453]=note: print w/replies, xml ) Need Help??


in reply to Bring back the smartmatch operator (but with sane semantics this time)!

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...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found