The following are the warnings you mentioned:
use strict;
use warnings;
my $str = q{"hello" 'world' 'foo' "bar"};
print $str =~ /(".*?")|('.*?')/g;
This is because your regular expression will match only one or the other of the capture groups. The other will not match and he will return undef.
The following is shown:
while ($str =~ /(".*?")|('.*?')/g) {
print "one = " . (defined $1 ? $1 : 'undef') . "\n";
print "two = " . (defined $2 ? $2 : 'undef') . "\n";
print "\n";
}
Outputs:
one = "hello"
two = undef
one = undef
two = 'world'
one = undef
two = 'foo'
one = "bar"
two = undef
To get the desired behavior, simply place the capture group around the entire expression.
print $str =~ /(".*?"|'.*?')/g;
source
share