http://qs321.pair.com?node_id=68971

   1: #! /usr/bin/perl -w
   2: #
   3: # david landgren
   4: # perlfiles -- print out the names of all perl scripts in the given directories
   5: # handy for backticking into vi or grep...
   6: 
   7: =for perlmonks
   8: [NB: this was posted in CUFP, which was probably the
   9: wrong place so it wound up here.
  10: 
  11: My Perl coding leans more to [id://66379|this] than
  12: [isbn://1884777791|that] but I am trying to get out of
  13: the habit.
  14: 
  15: Be that as it may, I have a ridiculously simple Perl
  16: script that I use dozens of times a day, and no doubt
  17: other people will find it useful. It simply prints out
  18: the names of all the files in a directory that are perl
  19: files. I can use this to backtick the list into <tt>vi</tt>
  20: or <tt>grep</tt>.
  21: 
  22: Have fun.
  23: =cut
  24: 
  25: use strict;
  26: use Getopt::Std;
  27: use vars qw/$opt_u/;
  28: 
  29: getopts 'u';
  30: 
  31: @ARGV = ('.') unless @ARGV;
  32: 
  33: my @files;
  34: foreach my $dir( @ARGV ) {
  35: 	opendir D, $dir or die "Cannot open directory $dir: $!\n";
  36: 	while( defined (my $file = readdir(D)) ) {
  37: 		next if $file eq '.' or $file eq '..';
  38: 		$file = "$dir/$file";
  39: 		open IN, $file or next;
  40: 		my $line = <IN>;
  41: 		close IN;
  42: 		if( $line =~ m{^#!\s*/usr/(?:local/)?bin/perl} ) {
  43: 			if( $opt_u ) {
  44: 				print "$file\n";
  45: 			}
  46: 			else {
  47: 				push @files, $file;
  48: 			}
  49: 		}
  50: 	}
  51: 	closedir D;
  52: }
  53: exit if $opt_u;
  54: 
  55: local $, = "\n";
  56: print sort(@files), "\n";
  57: 
  58: =head1 NAME
  59: 
  60: perlfiles
  61: 
  62: =head1 SYNOPSIS
  63: 
  64: B<perlfiles> [B<-u>] [directory] [...]
  65: 
  66: =head1 DESCRIPTION
  67: 
  68: Print the names of all perl scripts in a directory. If
  69: no directory is given the current directory is assumed.
  70: 
  71: The script opens each file, and looks at the first line
  72: to decide whether it looks like a shebang line that
  73: would launch perl.
  74: 
  75: =head1 OPTIONS
  76: 
  77: =over 5
  78: 
  79: =item B<-u>
  80: 
  81: Unsorted. Do not sort the files, rather, print out the
  82: filenames in directory order.
  83: 
  84: =back
  85: 
  86: =head1 EXAMPLES
  87: 
  88: C<perlfiles>
  89: 
  90: 	./bar
  91: 	./foo
  92: 	./rat
  93: 	
  94: C<vi `perlfiles`>
  95: 
  96: Edit all the perl files in the current directory.
  97: 
  98: C<vi $(grep -l 'use Socket' $(perlfiles /usr/local/bin))>
  99: 
 100: Edit all the perl files in /usr/local/bin that use the
 101: Socket module (assuming a bash- or ksh-like shell).
 102: 
 103: =head1 BUGS
 104: 
 105: Will be fooled by any script that uses a tricky shebang
 106: line, or a C<C:\perl\bin\perl.exe> path. In the latter
 107: case you probably don't have a real shell, so this
 108: script probably isn't of much use (and probably in the
 109: former as well for all I know).
 110: 
 111: =head1 COPYRIGHT
 112: 
 113: Copyright (C) 2001 David Landgren.
 114: 
 115: This script is free software; you can redistribute it
 116: and/or modify it under the same terms as Perl itself.
 117: 
 118: =head1 AUTHOR
 119: 
 120:     David "grinder" Landgren
 121:     eval {join chr(64) => qw[landgren bpinet.com]}
 122: 
 123: =cut