views:

367

answers:

4

In an SO answer daxim states:

@array ~~ $scalar is true when $scalar is in @array

to which draegtun replies:

From 5.10.1+ the order of ~~ is important. Thus it needs to be $scalar ~~ @array

How about a small primer on ~~ with link(s) to source(s) including the following specific questions: What is ~~? What is ~~ called? Why does the order matter in one version but not in a previous one?

Note that a good summary may not get all the details and can be hard to write. An introduction or primer would be very useful to save time for someone unfamiliar with ~~ while expanding the exposure of this Perlism.

+13  A: 

Smart Match, see perldoc perlsyn

Per a request in the comment, I'll give a little more: Smart Match is an operator for arbitrary data types that attempts to make sense of an equality test knowing nothing more than the types of arguments, many of the tests require complex operations like iteration and regex application

Evan Carroll
which in turn is.... what?
OscarRyz
Which in turn is... thoroughly detailed just behind that link.
hobbs
I guess it hard to explain in simple words. :-/
OscarRyz
@Support. Many copy-paste fans in SO.
Tom
I just think unless you have a question above and beyond the docs -- which is fine, they should be the first point of reference. I'd be happy to answer above and beyond "What is $foo", I'm just at a loss of words for where to start other than the contents of that link.
Evan Carroll
@Evan, and I'm with you. I would do the same on some questions. I was just hopping that it was something as simple as: *Is the operator for xyz which does, abcd*. I'm ok with the link ( I won't read it anyway I don't *perl* :P )
OscarRyz
I'll be glad to oblige such a request!! ;) updated the answer
Evan Carroll
+1 much better ;)
OscarRyz
+5  A: 

(stolen from Learn Perl): Binary "~~" does a smart match between its arguments.

http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail

What does it do? "It depends" mostly on the the type of the arguments provided. The page linked above has excruciating detail on what the variations are.

JasonTrue
Apparently answering "What is ~~? What is ~~ called? How about a small primer on ~~ with link(s) to source(s)" deserves a downvote. Granted, I didn't answer "Why does the order matter in one version but not in a previous one" but the page I linked to does, implicitly.
JasonTrue
+3  A: 

It's the smart match operator.

In general, when you want information about operators in perl, see perldoc perlop

Joe
+17  A: 

Answering specifically "why does the order matter in one version but not in a previous one": the smart match operator was badly designed in 5.10.0 in a way that made it difficult to use reliably, and made the given/when construct less useful than it could be, so the semantics were changed with 5.10.1 and all future versions will pretend that the 5.10.0 version never existed.

In the 5.10.1+ version of smart match, the left operand and the right operand to ~~ are always treated distinctly. Just as with the =~ regex match operator, the left side is the "subject" of the match, and the right side is the "pattern" to match against -- whether that be a plain scalar, a regex, an array or hash reference, a code reference, or whatever. The specifics are detailed pretty well in perlsyn.

You shouldn't worry about the 5.10.0 version at all unless you've already written code that depends on the 5.10.0 semantics (in which case, you should rewrite it to require 5.10.1, or else it will break on all future versions of perl).

hobbs
I lol'd. +1....
Paul Nathan
Isn't it funny, this sounds exactly the reason that I prophesied for not going the route of `features` pragma, and reverting back to the 5.8 way of versioning: `/usr/bin/perl5.08` isn't `/usr/bin/perl5.10`, which isn't even `/usr/bin/perl5.10.1`. Now, we're stuck having to enable `features` on a version of Perl that breaking is backwards compatibility with the only major feature addition in a major release, by way of a minor release... fuck my life.
Evan Carroll
@Evan: so just use 5.12 then.
Ether
@Paul Nathan really, it's not that funny. It's pretty bad. Fortunately, in the aftermath of 5.10.1 we've made a clear decision to be somewhat less stupid in the future, and we have the past year of development work to show for it :)
hobbs
+1 for answering the only part of the question not covered by RTFM.
Michael Carman
@Michael Carman: since ~~ does not work well in searches "What is ~~ called?" addresses how to find it in TFM.
C.W.Holeman II
Michael Carman
For reference here is the perldoc announcing the Smart Match changes at 5.10.1: http://search.cpan.org/~dapm/perl-5.10.1/pod/perl5101delta.pod#Smart_match_changes
draegtun