Keep It Simple, Stupid | |
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?? |
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):
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 Section
Meditations
|
|