#!/usr/bin/perl -w #) { chomp; $k = length($_); if ($k >= $min && $min > 0) { print "$. $k [$_]\n"; } if ($k > $longest) { $longest = $k; $string = $_; $line = $.; } } # Don't print anything if we haven't read anything. # Don't print a summary line if we haven't specified a minimum # length, i.e. haven't said "print anything longer than ___". if ($min == 0 && $line > 0) { print "$line $longest [$string]\n"; } close($fh); return; } # -------------------------------------------------------------------- # saferead($file): opens a file (or stdin) for reading. sub saferead { my $sname = (caller(0))[3]; my $file = shift; my $fh; if ($file eq '-') { unless (open($fh, "<&STDIN")) { warn "$sname: can't dup stdin: $!\n"; $fh = undef; } } else { unless (open($fh, '<', $file)) { warn "$sname: can't read $file: $!\n"; $fh = undef; } } return $fh; } #--------------------------------------------------------------------- # Print a usage message from the comments and exit. sub usage { my ($emsg) = @_; use Pod::Usage qw(pod2usage); warn "$emsg\n" if defined $emsg; pod2usage(-verbose => 99, -sections => "NAME|SYNOPSIS|OPTIONS"); } sub manpage { use Pod::Man(); my $parser = Pod::Man->new(); open(STDOUT, "| groff -T ascii -man | gcat -s | less") || die "groff\n"; $parser->parse_from_file($0); close STDOUT || die "$myname: can't close stdout: $!\n"; $? = 1 if $? == 255; # from die exit($?); } #--------------------------------------------------------------------- # Print the UUID, current version, or source location. sub myuuid { my $UUID = sprintf("%s", q$UUID: 736339b5-e4eb-3611-9ee7-a20d017cd505 $ =~ /UUID: (.*) /); print "$UUID\n"; exit(0); } sub version { my $VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/); my $DATE = sprintf("%s", q$Date: 2008/06/18 22:51:39 $ =~ /Date: (.*) /); print "$myname $VERSION $DATE\n"; exit(0); } sub where { my $SOURCE = print "$SOURCE\n"; exit(0); } #--------------------------------------------------------------------- __END__ =head1 NAME longest - find the longest line in a file =head1 SYNOPSIS longest [-hmuvw] [file ...] =head1 OPTIONS =over 4 =item B<-h> Print a brief help message and exit. =item B<-m> Print the manual page and exit. =item B<-u> Print the script UUID and exit. =item B<-v> Print the version and exit. =item B<-w> Print the source location and exit. =back =head1 DESCRIPTION B prints the line number, length, and contents of the longest string in a file, not counting the newline. If no files are entered on the command line, stdin is read. =head1 AUTHOR Karl Vogel Sumaria Systems, Inc. =cut