[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Something like "grepfirst"



Roland wrote:
> What is the shortest code for finding the first
> existing directory in a path of directories?

Functional programming to the rescue:


sub first_existing_dir {
  my $d=shift or die;
  -d $d ? $d : first_existing_dir(@_)
}

my $dir = first_existing_dir( 'dir1', 'dir2', $default );


What's fun is that the list of dirs could (with just a small amount
of work) be replaced by a function which produces directory names,
one per invocation.  (E.g. the "each" hash iterator.)

John Porter


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe