home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / languages / perl / examples / ren < prev    next >
Encoding:
Text File  |  1991-02-28  |  692 b   |  46 lines

  1. #!/usr/bin/perl
  2.  
  3. ($op = shift) || die "Usage: ren perlexpr [filenames]\n";
  4.  
  5. # First, get the list of filenames. Use @ARGV if specified.
  6. # Otherwise, do all files in the current directory if STDIN is
  7. # a tty, otherwise read the filenames from STDIN.
  8.  
  9. if (!@ARGV)
  10. {
  11.     if (-t)
  12.     {
  13.     @ARGV = ('*');
  14.     }
  15.     else
  16.     {
  17.     @ARGV = <STDIN>;
  18.     chop(@ARGV);
  19.     }
  20. }
  21.  
  22. # Now, expand wildcards in the argument list...
  23.  
  24. foreach (@ARGV)
  25. {
  26.     push(@a,<${_}>);
  27. }
  28.  
  29. @ARGV = @a;
  30.  
  31. # Now, rename each file in turn
  32.  
  33. for (@ARGV) {
  34.     $was = $_;
  35.     eval $op;
  36.     die $@ if $@;
  37.     rename($was,$_) unless $was eq $_;
  38. }
  39.  
  40. # Some useful subroutines...
  41.  
  42. sub cap {
  43.     tr/A-Z/a-z/;
  44.     substr($_,$[,1) =~ tr/a-z/A-Z/;
  45. }
  46.