submersible_toaster has asked for the wisdom of the Perl Monks concerning the following question:
Whilst experimenting perlishly with recursion , and reading many of the posts here at PM that cropup with recursion and File Find quite regularly, I have the following code that decends a directory tree, building a hash as it goes. Keys that are filenames hold a url as their value, Keys that are directories store a hashref of that directory's files &&|| directories.
I would like to determine if File::Find can be used for this, but I cannot figure out how to do this with the \&wanted sub.
#!/usr/bin/perl -w -T use strict; use Data::Dumper; my $urlbase='http://proxy/cgi-bin/getobj.pl?'; my $path = shift @ARGV or die; sub recurse { my $dir = shift; if ( '/' ne substr($dir,-1,1) ) { $dir = $dir.'/' }; my $base = substr($dir , length($path)); my %tree; opendir(DR , $dir); my @stuff=(readdir DR); closedir(DR); LOOP: foreach my $path ( @stuff ) { my $dot = substr ($path,1,1); next LOOP if ( $dot eq "" || $dot eq "." ); if (-f "$dir$path") { # Associate a file with a url $tree{$path}="$urlbase$base$path"; } if (-d "$dir$path" && ! -l "$dir$path") { # Decend into directory (ere's the recursion. my %glob = recurse( "$dir$path" ); $tree{$path}=\%glob; } } return %tree; } print Dumper recurse ( $path ) ; exit;
WTF am I using the hash for? ybiC already has suspicions, and this confirms them.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Everybody loves not using File::Find
by pg (Canon) on Nov 27, 2002 at 03:45 UTC | |
by submersible_toaster (Chaplain) on Nov 27, 2002 at 05:44 UTC | |
|
Re: Everybody loves not using File::Find
by tadman (Prior) on Nov 27, 2002 at 02:40 UTC | |
by UnderMine (Friar) on Nov 27, 2002 at 13:21 UTC | |
by submersible_toaster (Chaplain) on Nov 28, 2002 at 00:08 UTC | |
|
Re: Everybody loves not using File::Find
by John M. Dlugosz (Monsignor) on Nov 27, 2002 at 05:34 UTC | |
|
Re: Everybody loves not using File::Find
by BrowserUk (Patriarch) on Nov 27, 2002 at 01:53 UTC |