Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!rbj From: rbj@uunet.uu.net (Root Boy Jim) Newsgroups: comp.lang.perl Subject: Re: better head Message-ID: <1991May16.205446.15295@uunet.uu.net> Date: 16 May 91 20:54:46 GMT References: <1991May16.152356.3186@convex.com> Organization: UUNET Communications Services, Falls Church, VA Lines: 44 tchrist@convex.COM (Tom Christiansen) writes: >Here's a little head clone I once wrote out of frustration >with head's misbehaving on files with long lines. Piping to "head -#" is the same as piping to "sed #q". Sed probably has problems with long lines too, tho. > #!/usr/bin/perl > # > # head -- perl clone of head command, but without heads silly > # limits regarding line lengths. runs faster than > # C version, too! tchrist@convex.com > > $num = ($ARGV[0] =~ /^-(.+)/ && shift) ? $1 : 10; > die "$0: badly formed number: $1\n" unless $num =~ /^\d+$/; > > unshift(@ARGV, '-') unless $argc = @ARGV; # <> changes @ARGV > while (<>) { > if ($. == 1 && $argc > 1) { > print "\n" if $deja_imprime++; It's deja vu all over again :-) > print "=> $ARGV <=\n" ; > } So far, so good. > if ($. <= $num) { > print; > } else { > close ARGV; > } I think you're reading too many lines. Wouldn't "print; close ARGV if $. == $num" be better? > } I think you missed something tho. What happens if some of the files have less than $num lines? -- [rbj@uunet 1] stty sane unknown mode: sane