# get_last returns an array reference for efficiency, # so we need to de-reference; hence the @{} around # the subroutine call my @lines = @{ get_last("foo.log", 20000) }; sub get_last { my ($filename, $from_last) = @_; open FILE, $filename or die "Can't open $filename: $!\n"; my @lines = ; #loads the whole thing into memory close FILE; # return the last $from_last lines in an anonymous array # $#array_name is the index of the last element of # the array, so @lines[$#lines-20, $#lines] is the # last 20 elements of @lines [ @lines[$#lines-$from_last, $#lines] ]; }