home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Non-RPC / !Perl / scripts / SimpleGrep < prev    next >
Text File  |  1998-07-14  |  994b  |  56 lines

  1.  
  2. #! perl -w
  3. # This version of grep is fully featured, but is written for clarity not speed.
  4. use RISCOS::Throwback 'throwback_info';
  5. use RISCOS::Filespec;
  6. use RISCOS::File 'globlist';
  7. use Getopt::Std;
  8.  
  9. use vars qw($opt_c $opt_t $opt_v);
  10.  
  11. delete $SIG{'__DIE__'};
  12.  
  13. sub printit ($) {
  14.   print "$ARGV: " if ($lots);    # Only print the filename if more than one
  15.   print @_;
  16. }
  17.  
  18. if (defined $ARGV[0] and $ARGV[0] eq '-throwback') {
  19.     $throw = 1;
  20.     $pattern = shift;
  21. } else {
  22.     $throw = 0;
  23. }
  24. getopts ('ctv');
  25.  
  26. $pattern = shift @ARGV;
  27.  
  28. die "$0 [-throwback] [-c] [-v]  pattern [files]" unless $pattern;
  29.  
  30. @ARGV = globlist( @ARGV );
  31.  
  32. $count = 0;
  33. $throw = 0 if $opt_t;
  34.  
  35. $lots = @ARGV > 1;
  36.  
  37. while( <> )
  38. {
  39.   next unless $opt_v xor /$pattern/;
  40.  
  41.   $opt_c ? $count++ : printit ($_);
  42.   
  43.   if ($throw)
  44.   {
  45.       chomp;
  46.       throwback_info( $ARGV, $., $_ );
  47.   }
  48. } continue {
  49.   if (eof) {
  50.     printit ("$count\n") if ($opt_c && $count); 
  51.     $count = 0;
  52.     close(ARGV); # Reset line numbers for each file
  53.   }
  54. }
  55.  
  56.