Re: What's wrong with this script?

2006-11-05 Thread John W. Krahn
Tommy Nordgren wrote:
> What's wrong with this script for removing the CVS directories from a 
> checked-out CVS workspace?

Are you asking because it is not working correctly?


> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> my $mypath = '/Users/emac/gcc';
> 
> removecvs( $mypath);
> 
> sub removecvs {
> 
> my $path = $_[0];
> 
> system ('/bin/rmdir',"$path/CVS") or die "Can't remove CVS Directory  $!";

system() returns "false" on success and "true" on failure so you want to use:

system '/bin/rmdir', "$path/CVS" and die "Can't remove CVS Directory  $?";

Or:

system( '/bin/rmdir', "$path/CVS" ) == 0 or die "Can't remove CVS
Directory  $?";

But why not just use Perl's built-in rmdir() function:

rmdir "$path/CVS" or die "Can't remove CVS Directory  $!";


> my $dir;
> 
> opendir($dir,$path);

You should *ALWAYS* verify that the directory opened correctly:

opendir my $dir, $path or die "Cannot open '$path' $!"


> while (defined(my $file = readdir($dir))) {
> next if ( ($file eq '.') or ($file eq '..')) ;
> if (-d "$path/$file") {
> removecvs("$path/$file");
> }
> }
> 
> closedir $dir;
> }

I would probably do something like this:

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;

my $mypath = '/Users/emac/gcc';

my @directories;

find sub {
-d and $_ eq 'CVS' and push @directories, $File::Find::name
}, $mypath;

for my $path ( @directories ) {
rmdir $path or die "Can't remove '$path' $!";
}

__END__


Also, your code will only remove "$path/CVS" if it is empty.  If you want to
remove a "$path/CVS" that contains files you should use the rmtree() function
from the File::Path module.



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.   -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: What's wrong with this script?

2006-11-05 Thread Travis Thornhill
rmdir will only delete empty directories. I assume these directories contain 
files?
   
  Plus I'm not sure that using system() with die works the way you are 
intending. Why not use perl's rmdir (which also only deletes empty dirs).  I'd 
try opendir on the CVS
  directory and unlink all the files in it, then call rmdir() on the CVS 
directory.
   
  Try sticking print statements in it to try to see where it's hanging.
   
  HTH,
  - Travis.

Tommy Nordgren <[EMAIL PROTECTED]> wrote:
  What's wrong with this script for removing the CVS directories from a 
checked-out CVS
workspace?

#!/usr/bin/perl
use strict;
use warnings;

my $mypath = '/Users/emac/gcc';

removecvs( $mypath);

sub removecvs {

my $path = $_[0];

system ('/bin/rmdir',"$path/CVS") or die "Can't remove CVS Directory 
$!";

my $dir;

opendir($dir,$path);

while (defined(my $file = readdir($dir))) {
next if ( ($file eq '.') or ($file eq '..')) ;
if (-d "$path/$file") {
removecvs("$path/$file");
}
}

closedir $dir;
}

any help appreciated.
-
This sig is dedicated to the advancement of Nuclear Power
Tommy Nordgren
[EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





 
-
Cheap Talk? Check out Yahoo! Messenger's low  PC-to-Phone call rates.

Re: What's wrong with this script?

2006-11-05 Thread Tom Phoenix

On 11/5/06, Tommy Nordgren <[EMAIL PROTECTED]> wrote:


What's wrong with this script for removing the CVS directories from a
checked-out CVS workspace?


I don't know. What do you think is wrong with it? Does it do something
wrong? Does stepping through it with the debugger give you any clues?


opendir($dir,$path);


There's no check for success; opendir should use "or die" much as open does.

Other people may find other errors. Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]