home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / clones / wc4.pl < prev    next >
Encoding:
Perl Script  |  1991-07-12  |  985 b   |  68 lines

  1. #!/usr/local/bin/perl
  2. # wc in Perl - Bjorn.Larsen@usit.uio.noo
  3.  
  4. $l = 1;
  5. $w = 1;
  6. $c = 1;
  7.  
  8. while (($#ARGV >= 0) && ($ARGV[0] =~ /^-(.*)/)) {
  9.   $l = $w = $c = 0;
  10.   for (split(//, $1)) {
  11.     unless ($_ =~ /(l|w|c)/) {
  12.       print "Usage: wc [-lwc] [files]\n";
  13.       exit;
  14.     };
  15.    eval "\$$_ = 1";
  16.    shift;
  17.   };
  18. };
  19.  
  20. if ($#ARGV < 0) {
  21.   &count(STDIN);
  22.   exit;
  23. };
  24.  
  25. for (@ARGV) {
  26.   open(F, $_);
  27.   &count(F, $_);
  28. };
  29.  
  30. if ($#ARGV > 0) {
  31.   printf("%8d", $LINES) if $l;
  32.   printf("%8d", $WORDS) if $w;
  33.   printf("%8d", $CHARS) if $c;
  34.   print(" total\n");
  35. };
  36.  
  37. exit;
  38.  
  39. sub count {
  40.  local($fh, $f) = @_;
  41.  local($chars, $words, $lines);
  42.  
  43.  while (<$fh>) {
  44.    if ($c) {
  45.      $chars += length;
  46.      $CHARS += length;
  47.    };
  48.    if ($l) {
  49.      $lines++;
  50.      $LINES++;
  51.    };
  52.    if ($w) {
  53.      s/^\s*//;
  54.      $wc = split(/\s+/);
  55.      $words += $wc;
  56.      $WORDS += $wc;
  57.    };
  58.  };
  59.  
  60.  printf("%8d", $lines) if $l;
  61.  printf("%8d", $words) if $w;
  62.  printf("%8d", $chars) if $c;
  63.  
  64.  print(" $f") if $f;
  65.  
  66.  print "\n";
  67. }
  68.