Giter Club home page Giter Club logo

file-chdir-walkdir's Introduction

NAME

File::chdir::WalkDir

SYNOPSIS

use File::chdir::WalkDir

my $do_something = sub {
  my ($filename, $directory) = @_;

  ...
}

walkdir( $dir, $do_something, qr/^\./ );
# executes $do_something->($filename, $directory) [$directory is the folder
# containing $filename] for all files within the directory and all 
# subdirectories. In this case excluding all files and folders that 
# are named with a leading `.'.

DESCRIPTION

This module is a wrapper around David Golden's excellent module File::chdir for walking directories and all subdirectories and executing code on all files which meet certain criteria.

FUNCTION

walkdir( $dir, $code_ref [, @exclusion_patterns, $opts_hashref ]);

walkdir takes a base directory (either absolute or relative to the current working directory) and a code reference to be executed for each (qualifing) file. This code reference will by called with the arguments (i.e. @_) containing the filename and the full folder that contains it. Through the magic of File::chdir, the working directory when the code is executed will also be the folder containing the file.

Optionally exclusion patterns may by passed which will exclude BOTH files AND directories (and hence all subfiles/subdirectories) which match any of the patterns. This use is discouraged in favor of the exclude option below.

An optional hash reference, passed as the last option to walkdir will process key-value pairs as follows:

  • include - an array reference of inclusion patterns (i.e. qr//).

    If this option is not empty, files/folders will be skipped unless they meet one of these patterns. The are checked in order and short-circuit when a match is found. This check is executed before exclusion patterns are checked.

  • exclude - an array reference of exclusion patterns (i.e. qr//).

    If specified, files/folders which match any of these patterns will be immediately skipped. They are checked in order and short-circuit when a match is found. This check is executed after inclusion patterns are checked. This is a coarse exclusion. Fine detail may be used in excluding files by returning early from the code reference.

  • defer - when set to a true value, tells walkdir to process the files after listing the directory, recursing into the subdirectories, and excluding via the exclude patterns. This is less efficient, however may be necessary if the actions taken might confuse readdir. For example if changing the name of the file. This is only a per-directory defer however, moving files between directory might still be suspect.

  • act_on_directories - when set to a true value, tells walkdir to include the directory names as files to be acted on (subject to the normal exclusion mechanisms). For protection from infinite loops, directory actions are always deferred.

Note: walkdir will act on symlinked files but not on symlinked folders to prevent unwanted actions outside the folder and to prevent infinite loops. To exclude symlinked files too add a line like return if (-l $filename); near the top of the code to be executed; this is an example of the fine exclusion mentioned above.

SOURCE REPOSITORY

http://github.com/jberger/File-chdir-WalkDir

AUTHOR

Joel Berger, <[email protected]>

COPYRIGHT AND LICENSE

Copyright (C) 2011 by Joel Berger

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.