in reply to Removing Multiple Image Files

I have a handfull of comments to make. I hope they are helpful.

First, if($photo1 ne "") isn't very perl-y ... Try if($photo1). There's a lot of differnt kinds of false in perl, undef, and "" are among them.

Second, you should be really careful about embedding filenames in system() calls when they might have been written by some nefarious "customer." If you choose system("rm", "$photo/$cat$id/$thing") (i.e. multi arg call), then you don't have to worry about evil shell escapes. That could save you a lot of trouble one day.

Lastly, your actual question, I think can be solved with opendir:

opendir my $dir, "$photodir/$category" or die "didn't work: $!"; while( my $ent = readdir $dir ) { next unless $ent =~ m/something/; # unlink("$p/$c/$ent") or die "didn't work: $!"; } closedir $dir; }

Probably some people would also recommend File::Find (or a simple glob like the above) — and those solutions may be easier for you, but I like the old fasioned stuff. Actually, having re-read that it's 10 or so images I'd say the glob is probably best.

Update 2 ikegami: In point of fact, I usually use a variant of if(defined $something) myself. I objected to ne "" without thinking it through clearly.

-Paul

Replies are listed 'Best First'.
Re^2: Removing Multiple Image Files
by ikegami (Patriarch) on Jan 05, 2007 at 05:28 UTC

    First, if($photo1 ne "") isn't very perl-y ... Try if($photo1).

    Let's hope there's no photos named 0. An odd name, I'll grant you, but your suggestion weakens the code.

    ( By the way, it probably be best if $photo1 was undefined and the if was if (defined $photo1). It's easier to catch errors when using undef over the empty string to mark the absense of something, since using undef as a string leads to a warning. )