in reply to Regex variable capturing gotcha

It's a feature, see perlretut, perlre

Your code can be rewritten such way

while(<DATA>) { if( $_ =~ /(foo)/ ) { print "$count: $1 -- $&\n"; $count ++; } }