Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: File::Find redux: how to limit hits?

by crazyinsomniac (Prior)
on Jun 04, 2002 at 01:12 UTC ( [id://171372]=note: print w/replies, xml ) Need Help??


in reply to File::Find redux: how to limit hits?

use the 'preprocess ' option. Your other option is to mess with signals (see perlsig, %SIG)
#!/usr/bin/perl -w use strict; use File::Find; # scrap codeee find( { wanted => \&wanted, # preprocess => \&preprocess, }, '/' ); BEGIN { use vars qw( $maxHit $Hit); $maxHit = 10; } BEGIN { $SIG{__DIE__} = sub { my @d = @_; if($d[-1] eq "dying max $maxHit") { warn "hit the max $maxHit"; return(); } else { warn(@_); exit(1); } }; } sub wanted { my $file = $_; print "$file\n"; $Hit++; die "dying max $maxHit" if($Hit >= $maxHit) } ## something like this sub preprocess { my @list = @_; my $diff = $maxHit - $Hit; warn "pre processing( $Hit, $maxHit, ".scalar(@list)." )"; if($Hit > $maxHit ) { return (); }elsif( @list > $diff ) { warn "splicing";use Data::Dumper; @list = splice(@list,0,$diff); warn Dumper \@list; } return @list; }
update: you better stick with the eval ( see Kanjis node above). I've been too signal happy lately ;)

update: please note that the preprocess option is not available everywhere (best upgrade File::Find)

I also feel that there ought to exist File::Find::breakout, which would basically stop find from executing anymore (return? ;)

UPDATE: after hastily posting this, which i still think is good idea, a couple of smart asses point out goto. Yeah well i'm not 0 for 2 so far ;)(dammit! sometimes solutions you know but don't love escape you)

 
______crazyinsomniac_____________________________
Of all the things I've lost, I miss my mind the most.
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Replies are listed 'Best First'.
Re^2: File::Find redux: how to limit hits?
by Aristotle (Chancellor) on Jun 04, 2002 at 01:34 UTC

    crazyinsomniac++

    The preprocess mention is great - because now I can propose an addition to fix what had bugged me about 914's current implementation of the min/maxdepth behaviour: the fact that the files have to be looked at, and if for no other other reason than to discard them. preprocess lets one avoid that:

    sub prep { my $depth = $File::Find::dir =~ tr[/][]; return if $depth > $max_depth; return grep -d "$File::Find::dir/$_", @_ if $depth < $min_depth; @_; }

    This way, in directories below the mindepth, nothing other than directories is even looked at. Also further recursing down the current branch of three directory tree is aborted immediately upon entering a directory deeper than maxdepth.

    The mindepth test in wanted() is still necessary because the directories below mindepth will have to be processed; the maxdepth test there is superfluous.

    I'm also quite confident that this will cut the runtime down far enough that the maxhits kludges are unnecessary.

    Makeshifts last the longest.

Re: Re: File::Find redux: how to limit hits?
by u914 (Pilgrim) on Jun 04, 2002 at 17:27 UTC
    crazyinsomniac:
    That's exactly what i meant.... it (a 'breakout' option) would seem to be a very useful thing to have..

    Though it seems (from that list traffic you linked) that it's not typical for some duffer (read: me) to just modify a Module as storied and widespread as File::Find

    Anyhow, it looks to me as though the $File::Find::prune way mentioned by grinder might be the way to go...

    i'm pretty sure that the files i want will always be found first, since they're the most recent ones. Every time i've run this, the output array fills up in most-recentness order, on linux and solaris both. Clearly it's not alphabetical (as someone mentioned), and in fact i'm depending on this characteristic in another part of the script (there's a way to make it more robust, i'll do that later).

    i'm not sure i totally understand your code, but i'll study it when i get home and can play.... thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://171372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-18 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found