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

well im new to perl and i am having problem with coding it i have been studying and reading other code through out the net but didnt quiet understand.. is there anyone that can help me with this 3 question.. using the most simple code possible xD that will be really helpful 1.Search a file for a common user entered phrase. 2.Loop through each file within a directory for a user entered phrase. 3.Loop through a drive structure, examining each file for user entered phrase.

Replies are listed 'Best First'.
Re: File Analysis
by toolic (Bishop) on May 10, 2010 at 16:36 UTC
    1.Search a file for a common user entered phrase. 2.Loop through each file within a directory for a user entered phrase.
    Here is one way to print all lines of all files in the current directory which contain a phrase:
    use strict; use warnings; use File::Slurp qw(read_dir); my $phrase = shift; for my $file (grep { -f } read_dir('./')) { open my $fh, '<', $file or die "can not open $file: $!"; while (<$fh>) { print if /$phrase/; } close $fh; }

    See also perlintro (especially Files and I/O), File::Slurp and grep.

    The Monastery has its own Tutorials to complement those on the official documentation website: Getting Started with Perl

Re: File Analysis
by repellent (Priest) on May 10, 2010 at 18:00 UTC
Re: File Analysis
by JavaFan (Canon) on May 11, 2010 at 07:45 UTC
    Most simple code? Easy. Don't use Perl. Here are some shell one liners:
    1. grep PHRASE file
    2. grep PHRASE *
    3. grep -r PHRASE rootdir