Another perl coding challenge
Tom Adams
taa0 at cris.com
Fri Dec 29 12:08:28 CST 2000
Apparently Perl 5.005 doesn't like the variable directory handle $dh
(it doesn't seem to bother Perl 5.6). FWIW, I prefer Eugene's solution.
Here's a dirt-simple answer to Garrett's challenge. Any more interesting
ones?
Todd Rockhold wrote:
> ~sdpm~
> Probably not as fun as Garrett's "reversible NAND gate" problem, but here is
> one I have encountered. I have a test system (Perl 5.6) and a production
> system (Perl 5.005). I would like to run the tree walker on the production
> system.
>
> The following code walks a directory tree if run with Perl 5.6 but fails
> with earlier versions. How do I modify it so that it will work with Perl
> 5.005?
>
> ----------------------------------------------------------------------------
> --------------------------------------------------------------
>
> my $DIR = "//yogi/yogidata/OntoCHROMRun/TestRuns"; # contains well files
> my $it = make_treewalker($DIR, \&is_well_file, sub {@{$_[1]}});
> while (defined (my $f_it=$it->()))
> {
> # process the interesting file
> print "found file: $f_it\n";
> }
>
> sub make_treewalker #black magic to walk the directory tree
> {
> my @queue = shift();
> my ($is_interesting, $userparam) = @_;
> return sub
> {
> while(1)
> {
> return undef unless @queue;
> my $cur = shift @queue;
> if (-d $cur && opendir my $dh, $cur)
> {
> my @files = grep {$_ ne '.' && $_ ne '..'} (readdir $dh);
> push @queue, map {"$cur/$_"} @files;
> }
> return $cur if $is_interesting->($cur, $userparam);
> }
> }
> }
>
> sub is_well_file # what files do we care about?
> {
> return () unless -f $_[0] && -T $_[0]; # gotta be a simple file
> return () unless open FH, "< $_[0]"; # gotta be able to open it
> return ($_[0]) if $_[0] =~ /\.wel/i ; # gotta be a .WEL file
> return ();
> }
>
> ~sdpm~
>
> The posting address is: san-diego-pm-list at hfb.pm.org
>
> List requests should be sent to: majordomo at hfb.pm.org
>
> If you ever want to remove yourself from this mailing list,
> you can send mail to <majordomo at happyfunball.pm.org> with the following
> command in the body of your email message:
>
> unsubscribe san-diego-pm-list
>
> If you ever need to get in contact with the owner of the list,
> (if you have trouble unsubscribing, or have questions about the
> list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
> This is the general rule for most mailing lists when you need
> to contact a human.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 3GATE.PL
Type: application/x-perl
Size: 479 bytes
Desc: not available
Url : http://mail.pm.org/archives/san-diego-pm/attachments/20001229/f4da00bc/3GATE.bin
More information about the San-Diego-pm
mailing list