Goo::Grepper - Grep all the files in a directory for a pattern


Goo documentation Contained in the Goo distribution.

Index


Code Index:

NAME

Top

Goo::Grepper - Grep all the files in a directory for a pattern

SYNOPSIS

Top

use Goo::Grepper;

DESCRIPTION

Top

Grep through files trying to find a string.

METHODS

Top

find_files

find all the files in a directory that match a pattern

AUTHOR

Top

Nigel Hamilton <nigel@trexy.com>

SEE ALSO

Top


Goo documentation Contained in the Goo distribution.

package Goo::Grepper;

###############################################################################
# Nigel Hamilton
#
# Copyright Nigel Hamilton 2005
# All Rights Reserved
#
# Author:       Nigel Hamilton
# Filename:     Goo::Grepper.pm
# Description:  Grep all the files in a directory for a pattern
#
# Date          Change
# -----------------------------------------------------------------------------
# 15/06/2005    Auto generated file
# 15/06/2005    Needed to do fast backlink calculations for The Goo!
#
###############################################################################

use strict;

use Goo::Prompter;
use File::Grep qw(fdo);


###############################################################################
#
# find_files - find all the files that match a pattern in a directory
#
###############################################################################

sub find_files {

    my ($pattern, $directory, $suffix) = @_;

    my @files = glob "$directory/*.$suffix";
    my @filenames;

    fdo {
        my ($file, $pos, $line) = @_;    # iterate all given filenames
        if ($line =~ qr/$pattern/ &&     # if any line in a filename matches $pattern AND
            $files[$file] =~ /([^\/]+)$/
            ) {                          # we can extract just the filename
                                         # print "found match $1 \n";
            push @filenames, $1;         # push it to list of filenames
        }
        }
        @files;

    # print "found these filenames " . join("\n", @filenames);
    return @filenames;

}

1;


__END__