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

Hi,
I am attempting to remove a files *only* from non-pwd. These files are created automatically when I execute the the code (subroutine).
#!/usr/bin/perl -w use strict; use File::Remove; use Shell; my $some_value = the_subrotine(@array); sub the_subroutine { my @array = @_; # then execute code that returns a value # plus some temporary files in ~/tmp/ return $somevalue; # I need to remove these files, so this is what I did # with method from Shell module - failed find ("~/tmp/ -maxdepth 1 -type f -exec rm -v {} \;"); # With method from File::Remove - also failed # remove (~/tmp/ *~); }
Both of the approaches I tried failed to delete the files. I also tried to delete files outside subroutines also failed. Is there a correct way to do it? Or any better alternatives?
Regards,
Edward

Replies are listed 'Best First'.
Re: Removing Files from non-pwd with Shell and File::Remove
by jbrugger (Parson) on Apr 22, 2005 at 05:41 UTC
    eXile is right, but if your code generates the temp-files, why not keep track of their names, and unlink them by using somithing like
    foreach (@generatedTempFile) { unlink $_; }
    Then you don't spend time searching.
    If it failes, is it another script that writes them as another user? Check if the permissions are set correctly.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Removing Files from non-pwd with Shell and File::Remove
by eXile (Priest) on Apr 22, 2005 at 05:09 UTC
    To find a file in a directory you can use 'glob' and the '-f' operator, to remove a file once you know it's name you can use 'unlink'.
Re: Removing Files from non-pwd with Shell and File::Remove
by splinky (Hermit) on Apr 22, 2005 at 12:56 UTC
    Your code for removing the files is below your return statement.