in reply to Everybody loves not using File::Find

That "LOOP:" gave me the willies, so I wondered what it would look like tidied up:
use warnings; use strict; use Data::Dumper; my $urlbase = 'http://proxy/cgi-bin/getobj.pl?'; sub recurse { my ($basedir, $dir) = @_; $dir = $basedir unless (defined($dir)); $dir = "$dir/" unless ($dir =~ m#/$#); my $base = substr($dir, length($basedir)); my %tree; opendir(DIR , $dir); foreach my $path (readdir(DIR)) { next if ($path =~ /^\.\.?/); # . & .. my $dirpath = "$dir$path"; if (-f $dirpath) { $tree{$path} = "$urlbase$base$path"; } elsif (-d $dirpath && !-l $dirpath) { $tree{$path} = recurse($basedir, $dirpath); } } closedir(DIR); return \%tree; } my ($path) = @ARGV; unless (defined($path)) { print "Usage: $0 path\n"; exit(0); } print Dumper recurse ($path);

Replies are listed 'Best First'.
Re: Re: Everybody loves not using File::Find
by UnderMine (Friar) on Nov 27, 2002 at 13:21 UTC
    To append the '/' when required with a regex :-
    $dir=~s#(?<!/)$#/#;
    Also I would suggest :-
    next if ($path =~ /^\.\.?$/);
    So that '.filename' file/directories are not ignored if you want to map these too.

    Hope it helps
    UnderMine

      Argh! non skinny substitution delimiters :). Lemme see if I understand though.

      s#(?<!/)$#/#;

      means... if the character / is NOT occuring as the last char in the string, substitute a / for the match, BUT since this is a zero width match ?< then presumably "/" replaces "" at the end of the string?.

      Not being a regex guru, and knowing that (at least for me), you're only as good as your best reference!! , I used the Perl In a Nutshell regex reference to unwrap this. However, the only mention of ! , is as the !~ operator, or in situations like (?<!=...) or (?!...) , being hesitant to use code before I understand it, am I even close?

      evil groan I don't want to map .anything (should have been clearer in the 1st post) , I like the .invisibility - and shall not shirk it!