koacamper has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to figure out what I am doing wrong...I want to have the ability to remove sub dirs, along with the contents. The sub-dirs also contain .htaccess, .htgroup files. Here's what I've got...but doesn't work:
require 'cgi.pm'; use File::Path; $dir_location = "/home/sites/www.domain.net/web/test/"; %in = (); &ReadParse(%in); # Parse the data submitted by the user if (($in{'sub_dir'})) { $dir= $in{'sub_dir'}; rmtree(['$dir_location','$dir'], 1, 1);
Thank you for any help you can give me!

Replies are listed 'Best First'.
Re: Removal of A Directory
by japhy (Canon) on May 31, 2001 at 19:21 UTC
    You have quote-itis. First, you don't need to quote simple strings as hash keys:
    $dir = $in{sub_dir};
    Second, placing variables inside single quotes does not expand them to their value. And you don't need to use double quotes either:
    rmtree([$dir_location, $dir], 1, 1);
    But I have an odd feeling you mean to do:
    rmtree([ "$dir_location/$dir" ], 1, 1);


    japhy -- Perl and Regex Hacker
Re: Removal of A Directory
by bikeNomad (Priest) on May 31, 2001 at 19:27 UTC
    Hi koakamper,
    First, I hope that ReadParse handles responses like '../../../..' . (I know, of course it does, but you'd be surprised at the holes some people leave...)

    Second, you have single quotes around your vars, which will keep their values from being used.

    Third, it's not clear to me why you're removing two whole trees ($dir_location and $sub_dir), or how $sub_dir is actually a subdirectory of anything. The first argument to rmtree is a list of directories to remove; in your case, you'd get the directory "/home/sites/www.domain.net/web/test/" removed, as well as whatever was in $in{'sub_dir'}. This may not be what you want (might you want to concatenate them?).

    Fourth, you don't say how it's "not working". If this is really a quote from your text, the single quotes probably broke it. Looking at the code for File::Path, rmtree uses readdir, which shouldn't have any problems with "dot files" like .htaccess. What's the problem?