@@ -31,14 +31,14 @@ our @EXPORT_OK = ( @{ $EXPORT_TAGS{':all'} }, 'corf' );
3131our @EXPORT = qw(
3232
3333) ;
34- our $VERSION = ' 0.17 ' ;
34+ our $VERSION = ' 0.18 ' ;
3535
3636sub _standard_dir ($);
3737sub _dir (%);
3838
3939sub random_file {
40- my @params = my ($dir , $check , $recursive ) = _params_random_file(@_ );
41-
40+ my @params = my ($dir , $check , $recursive , $follow ) = _params_random_file(@_ );
41+
4242 return $recursive ? _random_file_recursive (@params )
4343 : _random_file_non_recursive(@params );
4444}
@@ -101,7 +101,7 @@ sub _random_file_non_recursive {
101101}
102102
103103sub _random_file_recursive {
104- my ($dir , $check ) = @_ ;
104+ my ($dir , $check , $recursive , $follow ) = @_ ;
105105
106106 my $i = 1;
107107 my $fname ;
@@ -118,7 +118,7 @@ sub _random_file_recursive {
118118 $fname = $f ;
119119 }
120120 };
121- find($accept_routine , $dir );
121+ find({ wanted => $accept_routine , follow => $follow } , $dir );
122122
123123 return $fname ;
124124}
@@ -147,19 +147,21 @@ sub _params_random_file {
147147 foreach (keys %args ) {
148148 / ^\- (d|dir|directory|
149149 c|check|
150- r|rec|recursive)$ /x or carp " Unknown option '$_ '" ;
150+ r|rec|recursive|
151+ f|follow)$ /x or carp " Unknown option '$_ '" ;
151152 }
152153
153154 my $dir = _standard_dir _dir %args ;
154155 my $check = $args {-c } || $args {-check} || sub {" always O.K." };
155156 my $recursive = $args {-r } || $args {-rec} || $args {-recursive};
157+ my $follow = $args {-f } || $args {-follow};
156158
157159 unless (!defined ($check ) or (scalar ref ($check ) =~ / ^(Regexp|CODE)$ / )) {
158160 die " -check Parameter has to be either a Regexp or a sub routine," .
159161 " not a '" . ref ($check ) . " '" ;
160162 }
161163
162- return ($dir , $check , $recursive );
164+ return ($dir , $check , $recursive , $follow );
163165}
164166
165167sub _standard_dir ($) {
@@ -183,12 +185,17 @@ File::Random - Perl module for random selecting of a file
183185
184186 my $fname2 = random_file(-dir => $dir);
185187
186- my $random_gif = random_file(-dir => $dir,
187- -check => qr/\.gif$/,
188- -recursive => 1);
188+ my $random_gif = random_file(
189+ -dir => $dir,
190+ -check => qr/\.gif$/,
191+ -recursive => 1,
192+ -follow => 1
193+ );
189194
190- my $no_exe = random_file(-dir => $dir,
191- -check => sub {! -x});
195+ my $no_exe = random_file(
196+ -dir => $dir,
197+ -check => sub {! -x}
198+ );
192199
193200 my @jokes_of_the_day = content_of_random_file(-dir => '/usr/lib/jokes');
194201 my $joke_of_the_day = content_of_random_file(-dir => '/usr/lib/jokes');
@@ -226,8 +233,6 @@ The simple standard job of selecting a random line from a file is implemented, t
226233
227234=head2 random_file
228235
229- =item random_file
230-
231236Returns a randomly selected file(name) from the specified directory
232237If the directory is empty, undef is returned. There are 3 options:
233238
@@ -283,12 +288,16 @@ So switching -recursive on, slowers the program a bit :-)
283288Please look to the C<File::Find > module for any details and bugs
284289related to recursive searching of files.
285290
291+ =item -follow (-f)
292+
293+ Follow symlinks when in recursive mode. See C<File::Find > for details.
294+ Default is not to follow.
295+
286296=item unknown options
287297
288298Gives a warning.
289299Unknown options are ignored.
290300Note, that upper/lower case makes a difference.
291- (Maybe, once a day I'll change it)
292301
293302=back
294303
0 commit comments