use strict; use warnings; use English qw( -no_match_vars ); ... while (my $record = <$input_fh>) { my @prices = $record =~ m/(\$\d{1,3}(?:,\d\d\d)*\.\d\d\b)/g; $number_of_prices = scalar @prices; $number_of_prices == 3 or warn "Incorrect number of prices ($number_of_prices) at" . " line $INPUT_LINE_NUMBER of file $input_file\n"; ... } exit 0;