m// を リストコンテキストで使う

始めてのPerlより。

たまーに使うことがありそうなので、忘れないようにメモがわり。

コード

use Data::Dumper;

my $text = "Barney Rubble Fred Flintstone Wilma Filntstone";

my ( $first, $second, $third ) = ( $text =~ /(\b\w+\b) \s \b\w+\b \s?/gx );
print "$first, $second, $third\n";

my @list = ( $text =~ /(\b\w+\b) \s \b\w+\b \s?/gx );
print Dumper \@list;

my %hash = ( $text =~ /(\b\w+\b) \s (\b\w+\b) \s?/gx );
print Dumper \%hash;

結果

Barney, Fred, Wilma
$VAR1 = [
          'Barney',
          'Fred',
          'Wilma'
        ];
$VAR1 = {
          'Wilma' => 'Filntstone',
          'Barney' => 'Rubble',
          'Fred' => 'Flintstone'
        };