Perl: How to delete files, folders and subdirectories using & ldquo; rm & rdquo;


I am trying to delete files and folders using my perl script. I'm using the "rm -rf $file" command to delete the file but its not doing anything. However, when i try the same command on the cmd line it works and deletes files just fine. Attached is the code snippet. Any help is appreciated.

for each $f (@files)
       system "rm -rf $f";

I tried this on a couple of machines and its the same problem. However, i tried using other system commands such as mkdir, tar etc and it works just fine. I have also tried unlinking but the same problem persists.

You can use for or foreach, but there is no for each. I've run the code, once I fixed this, the files were deleted.

Using use strict; would have found this, because 'each' would have shown up as a bare word.

Here is the code that I used:


use warnings;
use strict;

my @files = qw( a.txt b.txt c.txt );

for my $f (@files)
   system "rm -rf $f";

This successfully removed a.txt, b.txt and c.txt, so I guarantee that it is working code.

Use the perl debugger to inspect @files, and check the permissions on all files that you're trying to delete.