Reading input from files and directories in the command line arguments (like grep -R).

Discussion in 'Perl Misc' started by Adam, Apr 13, 2004.

  1. Adam

    Adam Guest

    A fairly normal thing to do in Perl is to read input from the files
    named in the command arguments and process line by line.
    while (<>) { process($_) ; }

    But I want to recurse down directories the way grep -R works. So far
    I've come up with this:

    while (@ARGV>0) {
    $filename = shift(@ARGV);
    print ($filename);

    if ( -d $filename ) {
    @list = `ls $filename` ;
    @list1 = map {chomp; $_ = $filename . "/" . $_ } @list ;
    unshift(@ARGV, @list1) ;
    }

    elsif ( -e $filename ) {
    open(INPUTFILE, $filename) ;
    while($line = <INPUTFILE>) {
    process($line) ;
    }
    close(INPUTFILE) ;
    }
    }


    It works but I can't believe it's the best way to do it. Any better
    suggestions?
     
    Adam, Apr 13, 2004
    #1
    1. Advertising

  2. Adam

    Paul Lalli Guest

    Re: Reading input from files and directories in the command linearguments (like grep -R).

    On Tue, 13 Apr 2004, Adam wrote:

    > A fairly normal thing to do in Perl is to read input from the files
    > named in the command arguments and process line by line.
    > while (<>) { process($_) ; }
    >
    > But I want to recurse down directories the way grep -R works. So far
    > I've come up with this:
    >
    > while (@ARGV>0) {
    > $filename = shift(@ARGV);
    > print ($filename);
    >
    > if ( -d $filename ) {
    > @list = `ls $filename` ;
    > @list1 = map {chomp; $_ = $filename . "/" . $_ } @list ;
    > unshift(@ARGV, @list1) ;
    > }
    >
    > elsif ( -e $filename ) {
    > open(INPUTFILE, $filename) ;
    > while($line = <INPUTFILE>) {
    > process($line) ;
    > }
    > close(INPUTFILE) ;
    > }
    > }
    >
    >
    > It works but I can't believe it's the best way to do it. Any better
    > suggestions?
    >


    Use the standard File::Find module:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use File::Find;

    sub wanted {
    if (-f){
    open INPUT, $_ or die "Cannot open $File::Find::name: $!";
    while (<INPUT>){
    # process $_;
    }
    close INPUT;
    }
    }

    find(\&wanted, @ARGV);
    __END__

    read `perldoc File::Find` for more info

    Paul Lalli
     
    Paul Lalli, Apr 13, 2004
    #2
    1. Advertising

  3. Adam

    Adam Guest

    On Tuesday 13 April 2004 16:49, Paul Lalli wrote:

    > Use the standard File::Find module:


    Excellent, thanks! (I don't know how I missed that when searching for a
    way to do it.)
     
    Adam, Apr 19, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,716
    Terry Hancock
    Apr 23, 2005
  2. Paul
    Replies:
    1
    Views:
    112
    Sebastian Hungerecker
    Apr 24, 2007
  3. blnukem

    Grep in sub directories

    blnukem, Apr 23, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    169
    Mothra
    Apr 23, 2004
  4. Adam Petrie
    Replies:
    8
    Views:
    307
    Adam Petrie
    Oct 11, 2004
  5. Replies:
    0
    Views:
    82
Loading...

Share This Page