I’m not used to perl at all and having issues with the syntax. How would I return “brown” in the following? From what I’m reading, this should work..
my $text = "the quick brown dog";
print "$text\n";
my $newvar = $text =~ m/quick (.*) dog/;
print "$newvar\n";
is an assignment in scalar context, and assigns either
1orundef.You want to make this assignment in list context
which assigns the captured groups from the regular expression.
The difference between scalar and list contexts is one of the trickiest things to get used to in Perl.
Note that captured groups from regular expressions in Perl also get assigned to the special variables
$1,$2, … . So you also could just have said