use 5.010; use strict; use Test::More tests => 2; my $string = 'foo bar'; my $regexp = qr{^ (?\w+) \W+ (?\w+) $}x; sub get_words_1 { if ($string =~ $regexp) { my @tmp = @+{qw/ word1 word2 /}; return @tmp; } return; } sub get_words_2 { if ($string =~ $regexp) { return @+{qw/ word1 word2 /}; } return; } my @expected = qw(foo bar); my @results1 = get_words_1(); my @results2 = get_words_2(); is_deeply(\@results1, \@expected); is_deeply(\@results2, \@expected);