Skip to content

Commit 5f63740

Browse files
committed
v0.18: Follow symlink option
1 parent 648d17a commit 5f63740

3 files changed

Lines changed: 30 additions & 17 deletions

File tree

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Revision history for Perl extension File::Random.
22

3+
0.18 2017, Sun Oct 22
4+
- follow option to follow symlinks
5+
36
0.17 Tue Aug 20
47
- changed random_line method that it detects also how many lines are wanted
58
* my ($line1, $line2, $line3) = random_line $fname

README

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
File::Random Version 0.17
1+
File::Random Version 0.18
22
=========================
33
NAME
44
File::Random - Perl module for random selecting of a file
@@ -23,7 +23,8 @@ SYNOPSIS
2323

2424
my $random_gif = random_file(-dir => $dir,
2525
-check => qr/\.gif$/,
26-
-recursive => 1);
26+
-recursive => 1,
27+
-follow => 1);
2728

2829
my $no_exe = random_file(-dir => $dir,
2930
-check => sub {! -x});

Random.pm

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ our @EXPORT_OK = ( @{ $EXPORT_TAGS{':all'} }, 'corf' );
3131
our @EXPORT = qw(
3232
3333
);
34-
our $VERSION = '0.17';
34+
our $VERSION = '0.18';
3535

3636
sub _standard_dir($);
3737
sub _dir(%);
3838

3939
sub 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

103103
sub _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

165167
sub _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-
231236
Returns a randomly selected file(name) from the specified directory
232237
If 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 :-)
283288
Please look to the C<File::Find> module for any details and bugs
284289
related 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
288298
Gives a warning.
289299
Unknown options are ignored.
290300
Note, that upper/lower case makes a difference.
291-
(Maybe, once a day I'll change it)
292301
293302
=back
294303

0 commit comments

Comments
 (0)