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

Ok, so below you will find all my code for the function, what it does is basically goes through the CWD and searches for all HTML files in it and the sub directories. I need help with is skipping one directory that i specify.

our $newdir = "Pulled on $date"; mkdir $newdir; # i want the dftree to skip the $newdir only and continue on # after. Thanks! sub dftree(){ my $dir = shift; opendir (DIR, $dir) or die "Could not open directory $dir: $!" +; my @entries = map {"$dir/$_"} grep {!/^\.{1,2}$/} readdir(DIR) +; closedir DIR; for (@entries) { (-d) ? &dftree($_) :(/\.html$/)? &file_check($_): next; } }
Update: Thanks Guys i will try both out and see which one works best for what i am doing, truly appricate it!

Replies are listed 'Best First'.
Re: Need help with Omitting a directory from this function
by graff (Chancellor) on Aug 26, 2015 at 21:35 UTC
    Given how that snippet is written, $newdir is in scope within the recursive function, so the function could just treat $newdir as an existing variable that has already been assigned a value.

    But your question is unclear, because you don't show how dftree is being called. If you are creating some new directory in the CWD and don't want it to be involved in searches, just get a listing of things to search in the CWD before you create that directory. If there's some pre-existing directory in the CWD that you need to avoid, build a list of things to search in the CWD, remove that one item from the list, and handle the rest of the list.

    If you're trying to exclude, say, "foobar" in the CWD, would you still be interested in handling anything called "foobar" at a lower level, or would you rather exclude everything called "foobar" no matter how deeply nested it might be?

Re: Need help with Omitting a directory from this function (file find rule ignore directory )
by Anonymous Monk on Aug 27, 2015 at 00:31 UTC

    How attached are you to this function? (is it homework)

    See Re^2: getting picky with File::Find::Rule less typing find/rule

    #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; use File::Find::Rule qw/ find rule /; path('gonetoday')->remove_tree; path($_)->touchpath for qw{ gonetoday/a/hi.html gonetoday/a/a.html gonetoday/a/b.html gonetoday/b/bye.html gonetoday/b/a.html gonetoday/b/b.html }; print "$_\n" for find( file => in => 'gonetoday' ); print "and no beee\n"; my $nobaby = rule( directory => name =>[ 'b', ], qw/ prune discard /); my $yeshtml = rule( file => name => qr/\.html/i ); print "$_\n" for rule()->or( $nobaby, $yeshtml )->in( 'gonetoday'); path('gonetoday')->remove_tree; __END__ gonetoday/a/a.html gonetoday/a/b.html gonetoday/a/hi.html gonetoday/b/a.html gonetoday/b/b.html gonetoday/b/bye.html and no beee gonetoday/a/a.html gonetoday/a/b.html gonetoday/a/hi.html