NAME

File::Find::Match - Perform actions on files matching a regexp.

SYNOPSIS

 use File::Find::Match qw( :all );
  my $matcher = match(
	  qr/\.pm$/ => sub {
		  print "Perl module: $_\n";
		  return SKIP;
	  },
	  qr/\.svn/ => sub {
		  # tell find() to ignore .svn dirs:
		  return IGNORE;
	  },
	  qr/\.pod$/ => sub {
		  print "Pod file: $_\n";
		  return DONE;
	  },
	  default => sub {
		  print "Other kind of file: $_\n";
	  },
  );
  find($matcher, '.');

DESCRIPTION

This module implements two functions, find() and match().

find() performs a breadth-first traversal of one or more directories, and match() creates a closure for use in performing various on actions depending on the filename.

EXPORT

None by default.

Export Tags

:all

same as :constants and :subs

:subs

find() and match()

:constants

DONE, SKIP, and IGNORE.

FUNCTIONS

The following functions are optionally exported.

find($visitor, @dirs)

Perform breadth-first directory traversal of all @dirs, calling the callback function reference $visitor for each file and directory seen. $visitor takes no arguments, and instead uses $_ for the each file name. $_ will have a (possibly relative) path prepended to it. find() does not call chdir().

Unlike File::Find, the return value of $visitor is important. If it is a false value, the current file will be skipped. This only really matters for directories, as it prevents them from being added to the queue.

match(%actions)

This function takes a list of key-value pairs. The keys are either regexp references from qr// or the string 'default'. The values are subroutine references.

match() will return a reference to newly-created function.

This function, which we will call visitor, that is returned will check run each regular expression on the value of $_, and if there is a match it will call the associated function reference. If this function ref returns SKIP, the next pattern will be tried. If it returns IGNORE, the visitor function will return undef. If it returns DONE, the visitor function will return a true value.

After all patterns have been tried, the visitor function will call the function reference associated with the 'default' string, unless we got IGNORE or the default action was not specified.

  match(
	  qr/\.pm$/ => sub {
		  print "Perl module! $_\n";
		  return SKIP;
	  },
	  qr/\.pod$/ => sub {
		  print "Pod file! $_\n";
		  return SKIP;
	  },
	  qr/\.pl$/ => sub {
		  print "Perl file! $_\n";
		  return DONE;
	  },
	  qr/\.svn/ => sub {
		  return IGNORE
	  },
	  default => sub {
		  print "Called for every file, unless ignored or done-ed.";
		  # make sure to return a true value here.
	  });
  
  # the above is functionally the same as...
  sub {
	  if (/\.pm$/) {
		  print "Perl module! $_\n";
	  }
	  if (/\.pod$/) {
		  print "Pod file! $_\n";
	  }
	  if (/\.pl$/) {
		  print "Perl file! $_\n";
		  goto default;
	  }
	  if (/\.svn/) {
		  return undef;
	  }
	  default:
	  print "Called for every file, unless ignored or done-ed.";
	  return 1;
  };

  # So, match() makes things a bit more elegant, no?

AUTHOR

Dylan William Hardison <dhardison@cpan.org>

http://dylan.hardison.net/

COPYRIGHT

Copyright (C) 2004 Dylan William Hardison.  All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.