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

Hi everyone I need to delete particular files from directory structure, So I'm using modified version of File::Found function which from perl monks as below
#!/usr/bin/perl use warnings; use strict; use diagnostics; use File::Find; sub eachFile { my $filename = $_; my $fullpath = $File::Find::name; if (-e $filename) { print "$filename exists!\n"; } if ($filename =~ /index.html.*/){ print "$filename need to be deleted\n"; system "rm $filename"; } } find (\&eachFile, "./");
examples of filenames are:
index.html index.html?C=D;O=D
etc. Output of given code comes out to be:
index.html?C=S;O=D exists! index.html?C=S;O=D need to be deleted rm: cannot remove `index.html?C=S': No such file or directory
I don't understand why its being so, I appreciate your suggestions

Replies are listed 'Best First'.
Re: not parsing properly??
by toolic (Bishop) on Apr 17, 2011 at 23:37 UTC
    Your system statement passes the string rm index.html?C=S;O=D to the unix shell. Since the semicolon has special meaning to the shell (it is a command separator), the shell breaks your string up into 2 commands:
    rm index.html?C=S O=D
    Since you do not have a file named "index.html?C=S", you get the "No such file or directory" message.

    Try to replace your system with unlink:

    unlink $filename or die $!;
    If that doesn't work, you may have to resort to some type of escaping heroics using backslashes or quotes.
      Awesome thanks a ton. That fixed it, Currently script is running. I'll let you know as its done deleting all junk :)
        yup cleaned and confirmed, thanks again :)
Re: not parsing properly??
by ikegami (Patriarch) on Apr 17, 2011 at 23:32 UTC

    You are creating a shell command without properly converting the file name into a shell literal.

    You could avoid the shell by using the multi-argument form of system, or you could delete the file using unlink instead of running another program.

Re: not parsing properly??
by anonymized user 468275 (Curate) on Apr 18, 2011 at 13:30 UTC
    In addition to the semicolon issue already mentioned, I think it is worth pointing out that the regexp covers a slightly larger range of possibilities than specifically intended. "." in a regexp means any character, so the "." between index and html needs to be escaped with a "\" to avoid, e.g. indexXhtml.* being included . Probably not important this time, but it might be another time.

    One world, one people