23

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.

Search strings: non-word-tilde-tilde non-word-at-sign.

Community
  • 1
  • 1
CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
  • 3
    With people giving minuses to almost every answer here, I'm kinda scared to even consider posting! :P – Jeriko Jun 22 '10 at 16:16
  • 3
    If you have a specific question to ask after reading the official documentation, please ask it. – Ether Jun 22 '10 at 16:51
  • `~~` is the secret inchworm operator which can be used to force scalar context: `~~localtime()` :-) – Eugene Yarmash Jun 22 '10 at 18:02
  • Quoting JS: "Downvoters: please provide comments. – Jon Skeet May 8 at 9:25" – CW Holeman II Jun 22 '10 at 18:21
  • @Ether: Google "Perl ~~" ignores the "~~". So, 1-What is ~~? 2-What is ~~ called? 3-Why does the order matter in one version but not in a previous one? 4-with link(s) to source(s) – CW Holeman II Jun 22 '10 at 18:23
  • Gosh, I thought the comment I gave had some deja-vu about it :) http://stackoverflow.com/questions/720482/how-can-i-verify-that-a-value-is-present-in-an-array-list-in-perl/720530#720530 – draegtun Jun 22 '10 at 19:06
  • 1
    @C.W.Holeman: are you really asking, or just restating the question? I think the downvoters might be reacting to your seeming unwillingness to simply type "perldoc perlsyn" at your command line. – Ether Jun 22 '10 at 20:42
  • The question was posted when the text "Smart Match" was unknown. It was provoked by a rather interesting note saying the order of ~~ matters now but not before. If the practice for SO would indicate that it would be best to make this multiple questions I would like to do that. – CW Holeman II Jun 22 '10 at 21:17
  • 1
    TFM links are to a paragraph that begins with "The behaviour of a smart match depends on". It is basically an appendix with a table of dozens of entries for the ~~ operator in reference to the switch feature. With a bit of hunting and care one may be able to determine if the smart match requires the switch feature. Does it actually work independent of switch? A nice introduction or primer would be very useful to save time for someone unfamiliar with the smart match while expanding the exposure of this Perlism. A couple of stand alone paragraphs would be nice or link if they already exist. – CW Holeman II Jun 22 '10 at 21:23
  • 2
    +1 - To those who down-voted this question, searching for `~~` in Google or on perldoc.perl.org results in no hits. Google is filtering it out, and perldoc just doesn't seem to find it (or "smart match" btw...). Unfortunately SO's search engine doesn't seem to like it either.... maybe a thread should be started in meta to discuss fixing this issue, since no characters should be out of bounds when searching in a programmer's QA site. – Eric Strom Jun 22 '10 at 23:54
  • @Eric Strom: Especially line noise from Perl. – CW Holeman II Jun 23 '10 at 00:10
  • 1
    @C.W.Holeman II => Exactly. I've opened a question about this on meta: http://meta.stackexchange.com/questions/54674/shouldnt-all-characters-be-searchable-on-a-programmers-qa-site – Eric Strom Jun 23 '10 at 00:51

4 Answers4

27

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
  • 223,387
  • 19
  • 210
  • 288
  • 3
    +1 for answering the only part of the question not covered by RTFM. – Michael Carman Jun 22 '10 at 17:17
  • @Michael Carman: since ~~ does not work well in searches "What is ~~ called?" addresses how to find it in TFM. – CW Holeman II Jun 22 '10 at 17:29
  • 2
    @C.W.Holeman II: Would you ask what `=~` is? How about `&&`? You could make the same "hard to search for" claim about those as well. Unless you're asking about pseudo-operators like `=()=` or obfuscations like `$x-->5` the answer is still the same: see perlop and perlsyn. – Michael Carman Jun 22 '10 at 17:57
  • 3
    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 Jun 22 '10 at 19:01
  • `~~@a` returns the size of array – where is “smart match” in such functionality? – Ωmega Jan 27 '23 at 14:34
  • @Ωmega that's not the `~~` operator, it's just the `~` prefix operator twice. – hobbs Jan 27 '23 at 14:39
19

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
  • 78,363
  • 46
  • 261
  • 468
  • 5
    Which in turn is... thoroughly detailed just behind that link. – hobbs Jun 22 '10 at 16:17
  • 2
    I guess it hard to explain in simple words. :-/ – OscarRyz Jun 22 '10 at 16:23
  • @Support. Many copy-paste fans in SO. – Tom Jun 22 '10 at 16:26
  • 3
    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 Jun 22 '10 at 16:39
  • @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 Jun 22 '10 at 16:43
  • I'll be glad to oblige such a request!! ;) updated the answer – Evan Carroll Jun 22 '10 at 16:51
5

(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
  • 19,244
  • 4
  • 34
  • 61
  • 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 Jun 22 '10 at 16:15
5

It is the smartmatch operator.

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

Zombo
  • 1
  • 62
  • 391
  • 407
Joe
  • 2,547
  • 1
  • 18
  • 27