in reply to Reading Lines from Text file

And then of course there is the lazy, unportable solution.
my $first = `head -1 $filename`; my $second = `tail -1 $filename`;
If you happened to want a solution regardless of the implementation (ie Perl or not), and were ignorant of the head and tail programs, then learn to use those.

Replies are listed 'Best First'.
Re^2: Reading Lines from Text file
by graff (Chancellor) on Feb 03, 2006 at 02:50 UTC
    Or, if you don't mind a little less portability in order to do one less sub-shell invocation:
    my ( $first, $last ) = `head -1 $filename && tail -1 $filename`;
Re^2: Reading Lines from Text file
by holli (Abbot) on Feb 02, 2006 at 19:44 UTC
    Why unportable? There are versions of "tail" and "head" for various OSses.


    holli, /regexed monk/
      Even if there are versions available for other OSses:
      • not every tail and head accept the same set of arguments
      • you may not have a compiler available to compile new tools
      • you may not able to install new software on the machine you're working,
      • Since you're using backticks, you're relying on whatever shell you have available, environment variables, aliases, (...)
      The safest way is to rely only on the Perl interpreter.