in reply to quick "open->close" file
If your file is large, it takes more effort.my @match = do { open my $fh, '<', $file or die "$!"; grep /^$ARGV[0]\|/, <$fh>; };
If I need the latter more than once, I'd probably factor it out:my @match; { open my $fh, '<', $file or die "$!"; local $_; my $rx = qr/^$ARGV[0]\|/ m/$rx/ && push @match, $_ while <$fh>; }
sub grep_file { my ($rx, $file) = @_; my @match; local $_; open my $fh, '<', $file or die "$!"; my $rx = qr/^$rx\|/ m/$rx/ && push @match, $_ while <$fh>; @match; }; my @match = grep_file($ARGV[0], $file);
Makeshifts last the longest.
|
|---|