Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (9)
    As of 2014-10-25 15:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (145 votes), past polls