perloperatorssmartmatch

Can I replace the binding operator with the smartmatch operator in Perl?


How can I write this with the smartmatch operator (~~)?

use 5.010;

my $string = '12 23 34 45 5464 46';

while ( $string =~ /(\d\d)\s/g ) {
    say $1;
}

Solution

  • If we go and look at what these two variants get transformed into, we can see the reason for this.



    The smart-match feature was never intended to replace the =~ operator. It came out of the process of making given/when work like it does.

    Most of the time, when(EXPR) is treated as an implicit smart match of $_.
    ...