Todd_Davies

Perl file commenter

Dec 30th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.93 KB | None | 0 0
  1. #
  2. #author: Todd Davies
  3. #use: Appends a comment to java, pl and txt files in a directory with my name!
  4. #warning: It's probably best not to use it on anything other than text based files!
  5. #
  6.  
  7. use Switch;
  8.  
  9. @files = <*>;
  10. foreach $filename (@files) {
  11.     my $new_fn = $filename . '.new';
  12.     open my $in, '<', $filename or die "Cannot open $filename for reading - $!";
  13.     open my $out, '>', $new_fn or die "Cannot open $new_fn for writing - $!";  
  14.    
  15.     my $ext = ($filename =~ m/([^.]+)$/)[0];
  16.     switch ($ext) {
  17.         case "txt"  { print {$out} "--------------------------\nauthor: Todd Davies\n--------------------------\n";   }
  18.         case "java" { print {$out} "/*\n *author: Todd Davies\n */\n";  }
  19.         case "pl"   { print {$out} "#\n#author: Todd Davies\n#\n";  }
  20.         else        {  }
  21.     }
  22.    
  23.     while (my $line = <$in>)
  24.     {
  25.         print {$out} $line;
  26.     }
  27.    
  28.     close($out);
  29.     close($in);
  30.     rename($new_fn, $filename);
  31.     print "Processed: ".$filename."\n";
  32. }
  33. <>;
Advertisement
Add Comment
Please, Sign In to add comment