NAME
File::Random - Perl module for random selecting of a file
SYNOPSIS
use File::Random qw/random_file/;
my $fname = random_file();
my $fname2 = random_file(-dir => $dir);
DESCRIPTION
This module simplifies the routine job of selecting a random file. (As you can find at CGI scripts).
It's done, because it's boring (and errorprone), always to write something like
my @files = (<*.*>);
my $randf = $files[rand @files];
or
opendir DIR, " ... " or die " ... ";
my @files = grep {-f ...} (readdir DIR);
closedir DIR;
my $randf = $files[rand @files];
FUNCTIONS
- random_file(-dir => $dir)
-
Returns a randomly selected file(name) from the specified directory If the directory is empty, undef will be returned.
Is the -dir option missing, a random file from the current directory will be used. That means '.' is the default for the -dir option.
EXPORT
None by default.
You can export the function random_file with use File::Random qw/random_file/;
or with the more simple use File::Random qw/:all/;
.
TODO
I think, I'll need some more options. Instead of only one directory, it should be possible to take a random file from some directories. Even a recursive "search" should be included.
More important will be the -check option, so you can define what regexp or subroutine should be valid, for files randomly choosen.
So I want to make it possible to write:
my $fname = random_file( -dir => '...', -recursive => 1, -check => qr/\.html/ );
or even:
my $fname = random_file( -dir => [$dir1, $dir2, $dir3, ...], -r => 1, -check => sub {-M < 7} );
I also want to add a method content_of_random_file
and random_line
.
The next thing, I'll implement is the -check option.
Just have a look some hours later.
COPYRIGHT
File::Random is free software. You can change or redistribute it under the same condition as Perl itself.
(c) 2002, Janek Schleicher
AUTHOR
Janek Schleicher, <bigj@kamelfreund.de>