in reply to Re^2: [5.10] =~ vs ~~
in thread [5.10] =~ vs ~~

Also ~~ is a boolean operator and perlsyn doesn't specify what it should do in list context

but when you add a use Data::Dumper; print Dumper \@a you can see that it does the same thing as =~ in list context.

What's really weird is that say scalar (my @a = ...) behaves differently from my @a = ...; say scalar @a;.

I think the doesn't represent the real matching code remark mostly means that the real matching code is optimized in many cases, but that it should semantically still do the same thing.

Replies are listed 'Best First'.
Re^4: [5.10] =~ vs ~~
by Arunbear (Prior) on Sep 01, 2008 at 10:47 UTC
    hmm, re behaviour in list context, I'm getting different results from you:
    use strict; use warnings; use Data::Dumper; use feature qw/say/; my @a1 = 'aaa' ~~ /./g; my @a2 = 'aaa' =~ /./g; say Data::Dumper->Dump([\@a1, \@a2], [qw/smart_match re_match/]);
    gives me
    $smart_match = [ 1 ]; $re_match = [ 'a', 'a', 'a' ];
    with v5.10.0 built for i686-linux
      I get the same result as you did (with blead), so I wonder what I messed up while testing.

      Anyway, I'll send a mail to p5p and ask for clarification.

Re^4: [5.10] =~ vs ~~
by blazar (Canon) on Sep 01, 2008 at 12:33 UTC
    What's really weird is that say scalar (my @a = ...) behaves differently from my @a = ...; say scalar @a;

    I personally believe that actually:

    $ perl -wMstrict -E 'say "@{[q|aaa| =~ /./g]}"' a a a $ perl -wMstrict -E 'say "@{[q|aaa| ~~ /./g]}"' 1

    FWIW, I realized this while writing the code for Finding differences in binary files: the line that starts with my @l was originally unnecessarily broken in two statements, the first of which was a match with ~~: then I thought I could condense them into one, but for some reason "it didn't work" - until I changed the operator to =~ "just to be sure," that is...

    --
    If you can't understand the incipit, then please check the IPB Campaign.