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

Hello monks!
I'm coding a program which ask the user for a search keyword.Then the program search this keyword inside *.html files which are at the current dir (my $dir = ".";). The problem is that the program searches only the current directory and not all the directories (if any) under current directory.
Can you please help me with that!?

Antonis!

Replies are listed 'Best First'.
Re: searching all directories with perl
by Aristotle (Chancellor) on May 04, 2003 at 14:01 UTC
    Use File::Find - it comes with Perl. This task would be even easier to do using File::Find::Rule, actually.
    my @matching_file = File::Find::Rule ->file() ->name( '*.html' ) ->grep( qr/\Q$keyword/ ) ->in( "." );

    Makeshifts last the longest.

(jeffa) Re: searching all directories with perl
by jeffa (Bishop) on May 04, 2003 at 14:27 UTC
    Aristotle++ on the quick draw! ;) He is dead on. File::Find and File::Find::Rule are the tools to use. The problem with processing all directories and not just the first one needs a solution called recursion. Recursion is a fairly advanced computer science technique that involves having a subroutine call itself. It can get quite tricky. Couple recursion with symbolic links and you can find your code getting 'stuck' in an infinite loop. Doing a simple Super Search on 'directory recurs' yielded a lot of good nodes, such as the famous How do I recursively process files through directories by the infamous Paco. There are more, read them. :)

    Also, there is a good tutorial on File::Find here at the monastery, and last year's Advent Calendar featured File::Find::Rule for the 11th day. Best of luck to you! :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Recursion is a fairly advanced computer science technique that involves having a subroutine call itself. It can get quite tricky.

      Huh? Fairly advanced? That's one of the first things you hear about in CS, I think. While I totally agree with the advice to use File::Find, I don't see a reason for advocacy against recursion in general. It can be a very elegant solution to solve problems in a simple and understandable way.

      Of course recursion, if used wrongly, can lead to endless loops etc. But that's not an argument against using it. It's an argument in favour of learning to use recursion the right way.

        Sorry, didn't mean to sound like i was arguing against recursion. Quite the contrary, recursion rules. I simply was pointing out that recursion is not easy, especially when first introduced to it. Remember, telling someone to use File::Find or File::Find::Rule is telling them to use recursion. ;)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)