CLOSE

A possible solution for the cabeza.pl program is as follows:

  1. #!/usr/bin/perl
  2.  
  3. # cabeza.pl - print the first 10 lines of a given file
  4.  
  5. use warnings;
  6. use strict;
  7.  
  8. # This is the first argument on the command line
  9. my $filename = shift(@ARGV);
  10.  
  11. # Check that file exists and is readable
  12. unless (-r $filename)
  13. {
  14. die "could not read file ($filename)";
  15. }
  16.  
  17. open IN, "<$filename"
  18. or die "open($filename) $!";
  19.  
  20. my $lineno = 0;
  21. while (<IN>)
  22. {
  23. $lineno++;
  24. exit if ($lineno > 10);
  25.  
  26. }
  27.  

CLOSE