DS has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys , I am trying to write a small script to descend the /clearb/pa/wBcn subdirectories, looking for *.cc files. When you find them, issue the following command:
make -f <source>.cpp
I am doing somthing like
`grep *.cc /clearb/pa/wBcn/*. > result.txt`; open (result , " result.txt" ) or die ""; while (<result>) { `make -f "$_\.cpp"`; } close(result);
but don't seem to do the work , not greping all .cc . thanks for any hints to solve this one ..

Replies are listed 'Best First'.
Re: greping in subdir
by krisahoch (Deacon) on Aug 19, 2002 at 15:26 UTC

    You may want to checkout out File::Find

    Also, your script my work in sh and bash, but it will probably fail in perl. try use Shell if you want to use shell commands. I would suggest not though

    Sorry that I don't have much else to offer, but I am busy atm:)

    Kristofer Hoch

    This node is a correction waiting for Abigail-II to happen ;-P~
Re: greping in subdir
by Abigail-II (Bishop) on Aug 19, 2002 at 15:56 UTC
    Why bother with Perl?
    $ find /clearb/pa/wBcn -name '*.cc' | sed -e s/.cc$/.cpp/ | xargs +make -f
    Abigail
      I am not sure but this wont do the subdirectories under wBcn!!!
Re: greping in subdir
by Bird (Pilgrim) on Aug 19, 2002 at 15:28 UTC
    Your best bet is probably going to be File::Find. It provides a nice interface for just this sort of problem.

    -Bird

Re: greping in subdir
by tomhukins (Curate) on Aug 19, 2002 at 16:02 UTC
    Others have suggested File::Find, but File::Find::Rule makes tasks like this easier. Try something like this (untested code):
    use File::Find::Rule; my @file = find( file => name => '*.cc', in => '/clearb/pa/wBcn' ) or die "Find Failed: $!"; foreach (@file) { s/\.cc$/.cpp/; @args = system('/usr/bin/make', '-f', $_); $args == 0 or die "system @args failed: $?"; }
    I also use perlfunc:system instead of backticks because you're not capturing the output of the make command. Also, this may be more secure because system with multiple arguments does not parse shell metacharacters such as * and ..