find2perl and File::Find examples

Discussion in 'Perl Misc' started by Sunil, Nov 28, 2003.

  1. Sunil

    Sunil Guest

    All,
    Where can I find good ( & simple) examples of find2perl and File::Find.
    The perldoc documentation is not very helpful (for me).


    Thanks,
    Sunil.
     
    Sunil, Nov 28, 2003
    #1
    1. Advertising

  2. Sunil

    Sunil Guest

    Sunil, Nov 28, 2003
    #2
    1. Advertising

  3. >>>>> "Sunil" == Sunil <> writes:

    Sunil> Where can I find good ( & simple) examples of find2perl and
    Sunil> File::Find. The perldoc documentation is not very helpful
    Sunil> (for me).

    It might help us if you elaborate the dividing line between the parts
    you understand and the parts you don't. In other words, what's your
    question to us?

    Do you understand find(1) already?
    Do you know what a coderef is?
    Do you know what a hashref is?
    Do you understand what a recursive filetree walk does?

    What task were you trying to accomplish that you failed at?

    See, when someone says "This isn't enough doc", I've got to wonder
    what they would want.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Nov 28, 2003
    #3
  4. On Fri, 28 Nov 2003 13:59:02 +0530, "Sunil"
    <> wrote:

    > Where can I find good ( & simple) examples of find2perl and File::Find.
    > The perldoc documentation is not very helpful (for me).


    I can't understand what the issue could be with the documentation.
    IMHO it's very clear. Do you have anything particular in mind?

    Also, examples of 'find2perl' are more or less "plain" examples of
    'find', with the exception that you can further customize the code
    beyond the limits of the 'find' utility.

    Personally, by seeing the code posted here every now and again I think
    that in many cases it could be worth (but is not done!) to use the
    \%options form of the function call and in particular the preprocess
    and postprocess "methods".

    I don't know it the following examples of mine are good (& simple) -
    for sure they are very naive (& old), but in any case here are my
    2cents:

    #!/usr/bin/perl -i.bak
    use strict;
    use warnings;
    use File::Find;

    die "Usage: $0 <dir> [<dir>...]\n" unless @ARGV;
    -d or die "`$_' either doesn't exist or is not a directory\n" for
    @ARGV;

    File::Find::find { preprocess => \&sel,
    wanted => \&extr }, @ARGV;

    sub sel { grep { /\.(?:asp|html?)/i or -d } @_ }

    sub extr {
    return unless -f;
    print "Processing `$File::Find::name'\n";
    local @ARGV=$_;
    s/(\.asp)\?/$1_/gi, print while <>;
    }
    __END__


    #!/usr/bin/perl
    use strict;
    use warnings;
    use File::Find;
    use File::Copy;

    our ($name, $dir);
    *name=*File::Find::name;
    *dir=*File::Find::dir;
    my %counts;

    die "Usage: $0 <dir> [<dir>...]\n" unless @ARGV;
    -d or die "`$_' either doesn't exist or is not a directory\n" for
    @ARGV;

    File::Find::find { preprocess => \&mkbakdir,
    wanted => \&bakfile,
    postprocess =>\&report }, @ARGV;

    sub mkbakdir {
    return if $_ eq '.bak';
    if (-e '.bak') {
    die "`.bak' exist in $dir but is not a directory\n"
    unless -d '.bak';
    } else {
    mkdir '.bak'
    }
    @_;
    }

    sub bakfile {
    return unless -f;
    my $dest='.bak/' . $_;
    unlink $dest or
    die "Can't remove previous backup copy of `$name': $!\n"
    if -e $dest;
    copy $_, $dest or
    die "Can't copy `$name' to .bak dir: $!\n";
    print "`$name' copied to .bak dir\n";
    $counts{$dir}++;
    }

    sub report {
    $counts{$dir}=0 unless defined $counts{$dir};
    print 'made backup copies for ', $counts{$dir},
    ' files in ', $dir, "\n";
    }
    __END__


    Michele
    --
    # This prints: Just another Perl hacker,
    seek DATA,15,0 and print q... <DATA>;
    __END__
     
    Michele Dondi, Nov 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed Doyle

    find2perl

    Ed Doyle, Dec 28, 2005, in forum: Perl
    Replies:
    1
    Views:
    3,874
    Joe Smith
    Dec 29, 2005
  2. Wybo Dekker
    Replies:
    1
    Views:
    374
    Yukihiro Matsumoto
    Nov 15, 2005
  3. Sunil

    help with find2perl

    Sunil, Nov 25, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    133
    Anno Siegel
    Nov 25, 2003
  4. nani
    Replies:
    2
    Views:
    170
    comp.llang.perl.moderated
    Mar 14, 2008
  5. GU
    Replies:
    4
    Views:
    342
    Peter J. Holzer
    Aug 31, 2009
Loading...

Share This Page