On 18/11/2014 22:46, Harry Putnam wrote:
I'll probably be battered for general poor perlmanship but still risking posting my whole script (fairly brief). The problem I'm having with it, that I don't see how to debug is that it breaks out on code inside Find.pm instead of breaking out on the if clause designed to catch the advent of user forgetting to supply a target directory. The `if (!@ARGV) {[...] bleh; exit}' thing. I'm pretty sure it is some other bit in there causing the grief but I don't understand what. Only thing I did to debug was to make sure `use File::Find; comes after if (!@ARGV), but that seems not to matter. If I run the script below without supplying a directory target I see: rmti.pl invalid top directory at /usr/share/perl/5.20/File/Find.pm line 472. ------- ------- ---=--- ------- ------- #!/usr/bin/perl use strict; use warnings; if (!@ARGV) { usage(); print "At least one directory name is required\n"; print "Usage tripped: line: <" . __LINE__ . ">\n"; exit; } elsif ($ARGV[0] eq "help") { usage(); print "Usage tripped: line: <" . __LINE__ . ">\n"; } my $myscript; ($myscript = $0) =~ s/^.*\///; ## BEGIN GETOPTS SECTION ========================================= # [HP 12/03/07_20:31:55 Don't use `use vars' anymore ] our ($opt_f); use Getopt::Std; my $optstr = "f"; getopts($optstr); my $FORCE; # expects boolean content if($opt_f) { ## force unlink ... no ask $FORCE = 'TRUE'; } my @trgs = @ARGV; @ARGV = (); for (@trgs) { if (! -d) { print "Whoops, no directory <$_> found on system .. skipping\n"; my $ditchme = shift @trgs; # Get rid of non-starters $ditchme = ''; ## zero it out ?? } } use File::Find; use Cwd 'abs_path'; find( sub { return unless -f; my $file = $_; my $abs_path = abs_path($file); if (/~$|\#|\.sw[po]$/) { if (! $FORCE) { print "\\FOUND: <$abs_path>\n", " Shall we unlink <delete> it? <y/n> "; my $ans = <STDIN>; if ($ans =~ /^y$/) { unlink $abs_path or die "Can't unlink <$abs_path>: $!"; printf "%-60s %s\n","<$abs_path>", "unlinked"; ## We need to use $_ rather than $File::Find::name because ## The latter doesn't work in all situations. And since the ## File::Find program is recursing right along with the search ## $_ will always work } else { printf "%-60s %s\n", "<$abs_path>", "skipped" } } else { unlink $abs_path or die "Can't unlink <$abs_path>: $!"; printf "%-60s %s\n", "<$abs_path", "unlinked"; } } }, @trgs ); sub usage{ print " Purpose: Recurse thru list of directories and search out all editor leavings (some~ .some.swp some.bak etc), but also includes things like #some# or the like. Usage:`$myscript dir1 dir2 ... dirN' "; }
There are a few issues with your code, but nothing that would cause the symptoms you describe. Are you sure you're running the code you think you are? Rather than if (!@ARGV) { ... } I would prefer to see if ( @ARGV == 0 ) { ... } which makes the intention much clearer. You also need to define `$myscript` *before* that `if` statement, as otherwise there is nothing for `usage` to use. It would be best written as my ($myscript) = $0 =~ m{([^/]+)$}; Finally, you need to ensure that `@trgs` isn't empty after removing any non-existent directories. I would write it like so my @trgs; for (@ARGV) { if (-d) { push @trgs, $_; } else { print "Whoops, no directory <$_> found on system .. skipping\n"; } } die "No valid directories found" unless @trgs; I'm sorry I can't be more helpful. Rob --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/