18

I've seen a number of comments across the web Perl's smart-match operator is broken. I know it originally was part of Perl 6, then was implemented in Perl 5.10 off of an old version of the spec, and was then corrected in 5.10.1 to match the current Perl 6 spec.

Is the problem fixed in 5.10.1+, or are there other problems with the smart-match operator that make it troublesome in practice?

What are the problems?

Is there a yet-more-updated version (Perl 6, perhaps) that fixes the problems?

Edit: To clarify, I'm not asking about bugs, but rather the opinion that the intended behavior of ~~ is bad.

Jonathan Cast
  • 217
  • 3
  • 10
Sean McMillan
  • 5,075
  • 25
  • 26
  • Wouldn't this be better off on SO? –  Nov 28 '11 at 22:34
  • 3
    So seems to hate questions that can't be answered with code snippets these days. – Sean McMillan Nov 28 '11 at 22:35
  • 2
    I don't think it belongs on SO, it asks for a conceptual explanation on why Perl's smart-match operator is considered broken. – yannis Nov 28 '11 at 22:41
  • 1
    From what I see in your link, it appears that smart-match is (or was) recursive, so it could probably lead to infinite recursion just because an array (possibly indirectly) contains a reference to itself. – user281377 Nov 28 '11 at 23:13
  • 2
    @SeanMcMillan - SO hates a lot of things these days :/ – Rook Dec 01 '11 at 16:08
  • @SeanMcMillan If you follow the link under 'Ricardo wants to fix smart matching' in your link, Ricardo Signes explains thoroughly why he considers it broken (`bizarre grammar exceptions in when() conditions`, `What I don't like is the massive number of things that go on the right-hand side of a ~~`, `nor the way that the dispatch is based on both the left and right sides`, `This table is very, very easy for anyone to memorize, unlike the current table.` and a few others)... – yannis Dec 01 '11 at 16:20
  • See my edit. It should answer your question. – Dynamic Dec 01 '11 at 20:01

2 Answers2

14

People often confuse the issues that arise with the smartmatch operator with those that arise with the given/when construct, such as the problem of lexical $_.

I’ve come to the opinion that if you use the smartmatch operator only on literals, that you will not go insane. I can make no promises about any other situation. Trying to figure out what $a ~~ $b is doing is enough to drive any decent human being to run screaming in madness, and to take up another programming language altogether.

Or at least, that’s the take I got from Larry when he was editing the smartmatch section of the 4th edition of Programming Perl.

tchrist
  • 341
  • 6
  • 11
4

The smart match operator wasn't "broken", but it had lots of bugs. Perl v5.12 fixed that, and Perl v5.14 perfected it IMHO. So, to answer your question, Perl v5.12 and up fixes this problem. You can try it with later versions of Perl v5.10, but I know it works better in Perl 5.12+.

Edit: To answer your edit, see this blog. After reading the article, I believe it sufficiently answered your question.

Dynamic
  • 5,746
  • 9
  • 45
  • 73