CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:



Nov 30, 2009, 3:28 PM

Post #1 of 3 (388 views)
system Can't Post

I am trying to write a script the will either delete or rename all of the subdirectories in a directory. If the subdirectory contains a file with a title like defects_AMD then I save the contents of that file and rename the subdirectory. Otherwise I delete the subdirectory. Here is the relevant code fragment:

my @dupsList; 
my @directories = <*>;
foreach my $directory (@directories)
my @defectsAMD;
if (-d $directory)
my @defectFiles = <$directory/defects_AMD*>;
open(DUPS_IN, "<", $defectFiles[0]) or die "Unable to open file '$defectFile': $!\n";
while (my $line = <DUPS_IN>)
push(@defectsAMD, "$line\n");
if (@defectsAMD)
push(@dupsList, @defectsAMD);
my $newName = $directory . "_unmerged";
system("mv", $directory, $newName);
system("rm -rf", $directory);

The system call in the if clause is succeeding and directories are being renamed. However, the system call in the else clause is not succeeding; the error message is:

Can't exec "rm -rf": No such file or directory at ../../

The file ../../ is the script that is running and it appears that it is looking to remove subdirectories in that directory. However, even when I specify the full path name to the subdirectories I want to remove I get the same error message. Also, I don't understand why one system call is finding the correct subdirectories and the other one isn't.

Later in the code I have:

open(DUPS_OUT, ">", $dupsFile) or die "Unable to open file '$dupsFile': $!\n"; 
print "Writing dups to file '$dupsFile'\n";
print(DUPS_OUT @dupsList);

But when I run another script that has to process $dupsFile I get this error:

The cursor is not positioned on a valid current row.

I am wondering if this means that the read position for that file is somehow set to the end of the file and, if so, how to fix it.

Any advice is appreciated.


Nov 30, 2009, 5:01 PM

Post #2 of 3 (385 views)
Re: [perlfarmer] system [In reply to] Can't Post

The error about the cursor can be ignored. It is not a Perl error.


Nov 30, 2009, 9:02 PM

Post #3 of 3 (380 views)
Re: [perlfarmer] system [In reply to] Can't Post

system('rm', '-rf', $directory);

If you use the multiple argument form of system(), you have to go all the way.

By the way, system() calls aren't free. Starting a separate process to execute a command has its own overhead. perl has all the necessary functions to accomplish what you are trying to do without resorting to system(), like the glob operator(you're actually using that but with the old syntax <*>), opendir(), readdir(), File::Path::rmtree(), and rename(). It may be that rm -rf is faster, but unless you know that, and you need the extra speed, stick to perl. The perl functions are going to be portable, while the commands you execute in your system() calls may not be.

(This post was edited by 7stud on Nov 30, 2009, 10:20 PM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives