use strict; use warnings; sub head { my ($cmd, $count) = (shift, shift || 10); my @output; open my $fh, "$cmd |" or die "error: '$cmd' failed"; while (<$fh>) { push @output, $_; last if $. == $count; } close $fh; return @output; } my @foo = head('cat very_large_file', 4); print @foo;