TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [TCLUG:2586] perl question
It might be better to test for arguments in a seperate foreach.  Here's
something I came up with quick, but it doesn't do '--recursive' or 
'--force', but that would be easy to add in.
'rm' it self doesn't allow you to start any filename with a '-'.  It gives
an error instead.  To get around this you can do 'rm ./-rf' when you need 
too.  This example works like that, since it exits if it doesn't know what
the argument is.
Hope this is helpful as a small example.
(There is also the getops perl module for this, and it works quite
well actually.  That's what I'd probably use.)
-Shawn
#!/usr/bin/perl
foreach $n (0..$#ARGV) {
  if ($ARGV[$n] =~ /^-/) {  # detects - at the beginning of the string
    $ARGV[$n] =~ s/-//g; # remove -
    if ($ARGV[$n] =~ /r/) {
      $RECURSIVE = 1;
      $ARGV[$n] =~ s/r//g; # remove r
    }
    if ($ARGV[$n] =~ /f/) {
      $FORCE = 1;
      $ARGV[$n] =~ s/f//g; # remove f
    }
   
    if ($ARGV[$n] !~ /^\s*$/) { # if it isn't just white space
      print STDERR "Unknown options: `$ARGV[$n]'\n";
      exit -1;
    }
  }
}
foreach $n (0.. $#ARGV) {
  if ($ARGV !~ /^\s*$/) { # it's not just white space
    do_it ($ARGV[$n]);
  }
}
sub do_it {
  my $arg = shift;
  print $arg . "\n";
}
--
Shawn T. Amundson               
amundson@gimp.org               http://www.gimp.org/~amundson
"The assumption that the universe looks the same in every
 direction is clearly not true in reality." - Stephen Hawking