Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

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

In reply to perlfiles - list all the perl scripts in a directory by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found