No, smart match operator cannot do this.
I thought, maybe, you could use the return value, but no, you can't, the return value of smart match operator is boolean (true/false).
My test code
$ perl -E " my( $r, $a, @bba ) = ( 0, 1, 2..4,1,2..4 ); $r = $a ~~ @b
+ba; say $r; "
1
You could accomplish this task by way of overload
#!/usr/bin/perl --
use 5.010;
use strict;
use warnings;
my @array = ( 2..4,6,2..4 );
say "@array";
my $r = 6 ~~ bless\@array,'smart::ix';
say "$r $$r = $array[$$r]";
BEGIN {
package smart::ix;
use overload '~~' => \&rix;
sub rix {
my( $self, $val, $selfOnRight ) = @_;
my $ix = 0;
while( $ix <= @$self ){
return \$ix if $val ~~ $self->[$ix];
$ix++;
}
return ;
}
$INC{'smart/ix.pm'} = __FILE__;
1;
}
__END__
2 3 4 6 2 3 4
SCALAR(0x9b386c) 3 = 6
This could make a good feature, say
use feature 'smarter'; makes ~~ return an object, and sets the global variable $s (for smart) to the same value (like sort does for $a and $b), and ->ix returns the index (for this use case), or ->matches for the general case ....
Hmm, now that I think about, after consulting http://search.cpan.org/~jesse/perl-5.14.1/pod/perlsyn.pod#Smart_matching_in_detail some more, ~~ seems rather limited , oh well |