Anchoring the Match

s/^\s+|\s+$//g;
my @headings = m/^=head\d\s+(.*)$/mg;