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

Hi ,
my $str = 'ban go ban'; my $count =()= ($str =~ /ban/g); print $count; my $count_again = ($str =~ /ban/g); print "\n$count_again";
can somebody explain me the difference in output

Replies are listed 'Best First'.
Re: regexp match count
by ikegami (Patriarch) on Oct 09, 2011 at 05:44 UTC

    The first match operator is called in list context. m//g in list context returns a list of all the captured strings or a list of all the matched strings if there are no captures.

    The second match operator is called in scalar context. m// (with or without 'g') in scalar context returns true if a match occurred or false otherwise.

    perlop

      Thnk you. It answered my doubt
Re: regexp match count
by eyepopslikeamosquito (Archbishop) on Oct 09, 2011 at 06:58 UTC

    my $count =()= ($str =~ /ban/g);
    This is the famous =()= goatse "secret operator", nicely explained here (along with Perl's other "secret operators").

Re: regexp match count
by davido (Cardinal) on Oct 09, 2011 at 05:16 UTC

    This should explain it.

    say( '$count = () = ( wantarray() ) gives ', ( $count = () = ( wantarray() ) ) ? "List" : "Scalar", " context to wantarray()." ); say( '$count = ( wantarray() ) gives ', ( $count = ( wantarray() ) ) ? "List" : "Scalar", " context to wantarray()." );

    The output:

    $count = () = ( wantarray() ) gives List context to wantarray(). $count = ( wantarray() ) gives Scalar context to wantarray().

    Dave