Interesting - in perl 5.10.1 on my system it works as expected.

One point to note, your "$myscript" in your usage should be escaped
otherwise you get an error.

You might want to:

use Data::Dumper;
print Dumper(@ARGV);

just before your test to see what it thinks @ARGV is actually set to...

Be interested to know what you find.

Regards,

Carl

On 18 November 2014 22:46, Harry Putnam <rea...@newsguy.com> 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'
> ";
> }
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to