in reply to Show different text based on random number
Everyone's problems are more interesting than mine. As long as I can write idiopathically. Idiomatically, I mean. I meant idiomatically.
use strict; # DO NOT OMIT. use warnings; # DITTO. my $dir = shift || die "Gimme a dir full of text files!\n"; my $random_texter = build_texter(dir => $dir); print $random_texter->(); exit; sub build_texter { require List::Util; require Path::Tiny; require Carp; my %arg = ( dir => "YOU FORGOT TO SET THE DIR!", suffix => "", # e.g., .txt @_ ); my $dir = Path::Tiny::path($arg{dir}); -d $dir && -r _ || Carp::croak("$dir is not a readable directory") +; my @files = grep /\Q$arg{suffix}\E\z/, grep -f, $dir->children; Carp::croak("There are no matching files in $dir") unless @files; sub { [ List::Util::shuffle( @files ) ]->[0]->slurp }; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Show different text based on random number
by kevbot (Vicar) on Apr 23, 2016 at 05:03 UTC |