CRAKRJACK has asked for the wisdom of the Perl Monks concerning the following question:

Does anybody know? This is definitely not the behaviour I expect. I could accept a syntax error, but this is just bizzre;

!~~
seems equivalent to
~~
... and there's more...

use 5.10.0; use strict; use warnings (FATAL => 'all'); my $a = "hello"; my $b = "hello"; print "a matches b\n" if($a ~~ $b); print "a not matches b\n" if($a !~~ $b); print "a matches not b\n" if($a ~~! $b); print "a matches and a half b\n" if($a ~~~ $b); print "a not matches and a half b\n" if($a !~~~ $b); print "a double matches b\n" if($a ~~~~ $b); print "a not double matches b\n" if($a !~~~~ $b);

This yields;

a matches b
a not matches b
a double matches b
a not double matches b

Does anybody know why?

Replies are listed 'Best First'.
Re: Why is to match, not to match?
by BrowserUk (Patriarch) on Aug 28, 2009 at 18:12 UTC

    Maybe this will clarify things for you? (Hint: $a !~~ $b parses as $a !~ (~$b))

    c:\test>perl -MO=Deparse use 5.10.0; use strict; use warnings (FATAL => 'all'); my $a = "hello"; my $b = "hello"; print "a matches b\n" if($a ~~ $b); print "a not matches b\n" if($a !~~ $b); print "a matches not b\n" if($a ~~! $b); print "a matches and a half b\n" if($a ~~~ $b); print "a not matches and a half b\n" if($a !~~~ $b); print "a double matches b\n" if($a ~~~~ $b); print "a not double matches b\n" if($a !~~~~ $b); ^Z sub BEGIN { require 5.10.0; } BEGIN {${^WARNING_BITS} = "\377\377\377\377\377\377\377\377\377\377\37 +7?"} use strict 'refs'; BEGIN { $^H{'feature_say'} = q(1); $^H{'feature_state'} = q(1); $^H{'feature_switch'} = q(1); } my $a = 'hello'; my $b = 'hello'; print "a matches b\n" if $a ~~ $b; print "a not matches b\n" if not $a =~ ~$b; print "a matches not b\n" if $a ~~ !$b; print "a matches and a half b\n" if $a ~~ ~$b; print "a not matches and a half b\n" if not $a =~ ~~$b; print "a double matches b\n" if $a ~~ ~~$b; print "a not double matches b\n" if not $a =~ ~~~$b; - syntax OK

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Yes, it does, thanks! .....except for this scary bit;

      print "a double matches b\n" if $a ~~ ~~$b; print "a not double matches b\n" if not $a =~ ~~~$b;

      That sounds like even perl does not know what it is doing!

      $a ==== $b
      would never make it past the censors, and it is telling us that it just successfully parsed a "match and a half" operator there?

        .....except for this scary bit;

        Not so scary once you realise that ~$b is bitwise-NOT of $b; and ~~$b is the bitwise-NOT of the bitwise-NOT of $b--which is just $b--and so on ad nauseum:

        c:\test>perl -E"$b = 'fred'; say for $b, ~$b, ~~$b, ~~~$b, ~~~~$b, ~~~ +~~$b" fred ÖìÜø fred ÖìÜø fred ÖìÜø
        That sounds like even perl does not know what it is doing!

        Perl knows! You just haven't been inducted into its inner circle yet:)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.