#!/usr/bin/perl use strict; use warnings; =head1 NAME countln - recurse directories and counts lines in matching files =head1 SYNOPSIS F S> S> =head1 OPTIONS =over =item B<-e>, B<--ext> What extensions to match. Can be given multiple extensions separated by commata, and can be given multiple times. If none given, defaults to F<.pm> and F<.pl>. =item B<-r>, B<--root>, B<--dir> Which directory to start recursing in. Defaults to the current directory =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";