Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

File::Random module

by gautamparimoo (Beadle)
on Apr 02, 2013 at 06:04 UTC ( [id://1026586]=perlquestion: print w/replies, xml ) Need Help??

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

Hi.. I need to know why is this code is not working. I am on Perl 5.14 and windows 2008 R2.

use File::Random qw/:all/; $dir = "E:\\"; my $file = random_file( -dir => $dir, -check => qr/.../, # or sub { .... } -recursive => 1 # or 0 ); print $file; OUTPUT:Can't stat E:: No such file or directory at C:/Perl/site/lib/File/Random.pm line 121

The $dir works for the default directory(the directory from which it is invoked)by specifying "." . But is gives this error if i try to specify the directory as E: or anything else which are valid paths. Anybody knows Why?

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

Replies are listed 'Best First'.
Re: File::Random module (bug)
by Anonymous Monk on Apr 02, 2013 at 07:12 UTC

    Anybody knows Why?

    Bug in File::Random, it does't use File::Spec or another file path module, it rolls its own code, and naturally its linux-centric

    I wouldn't use this module, look inside, all it does is call File::Find

    I would use Path::Iterator::Rule

      Can you show the bug? I'm a bit surprised as the module runs well in a little test case I made on Windows. The only thing which wonders me is, that File::Find's path seperator seems to be '/' in any case. Have a look at while running under Windows:

      #!/usr/bin/perl use strict; use warnings; use File::Find; find(\&wanted, 'C:\\Temp\\something'); sub wanted { print "$_\n"; print "$File::Find::name\n"; }

      @gautamparimoo: I can adress a drive 'Z:\' here on my installation. Silly question, but are you sure you have a 'E:\'? Show us the whole script and we can probably see where the problem is.

      McA

        @McA

        What i have shown is the only script i am trying coz i have not integtated it still because the code snippet is not working.Also I have seen than when I input C: drive(which is the drive from which the script is being run) it gives the correct output But the returned filenames are not from the whole C: drive just the subdirectory from where I am running the script. So when i input drive as E: than i get the same error as shown above. It is kind of a strange thing. Did u run it and what did u get?

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

        Can you show the bug?

        Yes I can, but I'm not interested, the OP already showed the bug

        Pay attention to the module name McA

Re: File::Random module (Path::Iterator::Rule)
by Anonymous Monk on Apr 02, 2013 at 10:21 UTC

    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; }

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found