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

Putting it as briefly as possible:

C:\temp>perl -wMstrict -E "say scalar (my @a = 'aaa' =~ /./g)" 3 C:\temp>perl -wMstrict -E "say scalar (my @a = 'aaa' ~~ /./g)" 1

Documented? Why so, anyway?

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

Replies are listed 'Best First'.
Re: [5.10] =~ vs ~~
by moritz (Cardinal) on Sep 01, 2008 at 09:41 UTC
    Since @a contains the same thing in both cases I think it's a bug. perlsyn says
    Any Regex pattern match $a =~ /$b/

    So they should be equivalent.

    (The bug is still in blead)

    Update: it's now on p5p.

      perlsyn also says
      The "matching code" doesn't represent the real matching code, of course: it's just there to explain the intended meaning.
      Also ~~ is a boolean operator and perlsyn doesn't specify what it should do in list context (though admittedly it doesn't seem that smart if it can't do-what-you-mean).
        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.

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

        I personally believe that in Perl 6 it is going to substitute =~ altogether, though, and while this is certainly not the case in Perl 5, one would still perlishly and dwimmily expect its return value to be richer than that...

        --
        If you can't understand the incipit, then please check the IPB Campaign.
Re: [5.10] =~ vs ~~
by ikegami (Patriarch) on Sep 01, 2008 at 12:36 UTC

    In addition to "~~" apparently only returning true or false, regexp matches made by "~~" appear to be performed in scalar context.

    >perl -lE"my @a1 = 'aaa' ~~ /.(?{ print 'X' })/g;" X >perl -lE"my @a1 = 'aaa' =~ /.(?{ print 'X' })/g;" X X X