Okay, since we’re all nitpicking, how about using a better file system iterator module? And some more gravvy?
#!/usr/bin/perl use strict; use warnings; =head1 NAME countln - recurse directories and counts lines in matching files =head1 SYNOPSIS F<countln> S<B<[ -e ext1,ext2 ]>> S<B<[ -r rootdir ]>> =head1 OPTIONS =over =item B<-e>, B<--ext> What extensions to match. Can be given multiple extensions separated b +y commata, and can be given multiple times. If none given, defaults t +o F<.pm> and F<.pl>. =item B<-r>, B<--root>, B<--dir> Which directory to start recursing in. Defaults to the current directo +ry =head1 SEE ALSO find(1), wc(1) =head1 BUGS None known. =head1 AUTHORS ... =head1 COPYRIGHT AND LICENCE ... =back =cut use Getopt::Long; use Pod::Usage; use File::Find::Rule; GetOptions( 'h|help' => sub { pod2usage( -verbose => 1 ) }, 'man' => sub { pod2usage( -verbose => 2 ) }, 'ext|e=s' => \( my @opt_ext ), 'root|dir|r=s' => \( my $opt_root = "." ), ) or pod2usage(); @opt_ext = @opt_ext ? map { split /,/ } @opt_ext : qw( pl pm ); my @file = File::Find::Rule ->file() ->name( map "*.$_", @opt_ext ) ->in( $opt_root ); my $lines = 0; for my $fname ( @file ) { open my $fh, '<', $fname or warn( "Couldn't open $fname: $!\n" ), next; local $/ = \131072; $lines += tr/\n// while <$fh>; $lines++ if not /\n\z/; } print "$lines lines in " . @file . " files\n";
Makeshifts last the longest.
In reply to Re: Count file lines in a directory tree
by Aristotle
in thread Count file lines in a directory tree
by GrandFather
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |