in reply to File::Random module

From http://search.cpan.org/perldoc/Path::Iterator::Rule#SYNOPSIS, you add whatever checks you need, same algorithm as File::Random, it reads the whole directory tree but only one filename at a time in memory

sub random_file { my @dirs = @_; my $rule = Path::Iterator::Rule->new; # match anything $rule->file->size(">10k"); # add/chain rules # iterator interface my $next = $rule->iter( @dirs ); my $random_file; my $i = 0; while ( my $file = $next->() ) { if( rand $i++ < 1 ){ $random_file = $file; } } return $random_file; }

Replies are listed 'Best First'.
Re^2: File::Random module (Path::Iterator::Rule)
by gautamparimoo (Beadle) on Apr 02, 2013 at 10:40 UTC

    Thnks.. But one question .How is this rand funtion working ie what does the following statement do:

    if( rand $i++ < 1 )

    Sorry , I just got what it was trying to do.

    One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man. -Elbert Hubbard

      If you don't understand something read the documentaiton and make a short example to illustrate what it's doing in order to better understand what's going on.

      my $i=0; my $x = rand $i++; print "$x\n";

      Update: typo

        I understood the working of rand function. It calculate a random number which is checked against the if condition and if passed than the filename is given. But what if I want to specify to generate atleast 50 or 100 random files?

        One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man. -Elbert Hubbard