Front page | perl.perl5.porters |
Postings from December 2017
Revisiting smart match
December 28, 2017 17:26
Revisiting smart match
Message ID: CAG2CFAZJuaTRBSV6jd6-_37E2eDNsngvmK7Xkok04u0pWWTjTQ@mail.gmail.com
I've been writing perl for $ since 1993, a bit before Perl 5. One of the
things I appreciated about the 5.0 release was, even with all the new
features and wonderful syntax, the guarantee of compatibility. Perl 4's
"$package'foo" variables didn't have to become "$package::foo" when
updating to Perl 5. And my client could keep using my code on a new box
without my having to edit all the package references. Plus, I could keep
using "mailagent" to sort my mail with a new perl, until Rafael got around
to updating that too.
I appreciate this push for an easy-to-understand, useful "switch"
statement. s there another way to solve the issues of "given/when/~~" that
lets old code keep running under the new perls? Please also understand that
Perl's compatibility and stability from version to version is a strong
selling point. You aren't bound to Larry's 90's ideal of yesterday's code
running forever, but when it comes to "given/when/~~," understand this perl
hacker's POV in 2007-
"Yay we got a switch statement! These first few rules look OK... then it
gets complicated... so I'll stick to the simple ones."
And I take advantage of "given / when" to test a value against multiple
constant options, and "if ($item ~~ $arrayref)" to test for set membership.
They are easy to read and work great (as a bonus the set membership test
beats "grep" in my Benchmark test.)
From my POV, as a programmer, smartmatch and for(given)/when were
infrequently wanted, and took some care to use, but very handy once in
place. They ended up in a fair amount of diverse projects over the last
decade. Much of my "DarkPan" relies on it. My old customers are using it
unknowingly with warnings going to log files that no one reads anymore.
Smartmatch, given/when were core language for six years, and when it was
declared "experimental" in 2013, there wasn't a clear replacement to
migrate to, so I turned on the experimental pragma and kept running with it.
Those features are far from perfect-- don't let the perfect be the enemy of
the good! Is there room for given/when/~~ to keep being "good enough" for
what they are do in existing code, while you're coding up something even