My solution is similar to Manni's, but I recommend using while
to read a file line-by-line. You can use substr() like he does, but a regex with anchors and without quantifiers is going to be pretty fast:
my @barcodes;
while( <$fh> )
{
next unless m/^Barcode:\*([0-9]{5})/;
push @barcodes, $1;
}
Depending on what else I was doing, I might just use a map instead. The map expression is in list context, so the m// operator returns the list of things it matched in any parentheses:
my @barcodes = map { m/^Barcode:\*([0-9]{5})/ } <$fh>;
I suspect any real-life answer would have a bit more code to warn you about lines that start with Barcode:
but are missing the number. I have yet to meet a perfect input file :)
The \G anchor picks up the regex matching where you left off with the last match on the same string, in this case right after the colon:
my @barcodes;
while( <$fh> )
{
next unless m/^Barcode:/;
unless( m/\G\*([0-9]{5])/ )
{
warn "Barcode is missing number at line $.\n";
next;
}
push @barcodes, $1;
}