Advertisement
swaggboi

mktxt.pl

Jan 11th, 2021
1,814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.13 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. # Convert scripts to text files (or back to scripts)
  4. # Daniel Bowling <dbowling@akamai.com>
  5. # Jan 2021
  6.  
  7. use strict;
  8. use warnings;
  9. use utf8;
  10. use open qw{:std :utf8};
  11. use Data::Dumper; # Uncomment for debugging
  12.  
  13. ## Variables ##
  14.  
  15. my @files = @ARGV;
  16. my $text  = <<'EOT';
  17. Whenever I get a package of plain M&Ms, I make it my duty to continue
  18. the strength and robustness of the candy as a species. To this end, I
  19. hold M&M duels. Taking two candies between my thumb and forefinger, I
  20. apply pressure, squeezing them together until one of them cracks and
  21. splinters. That is the “loser,” and I eat the inferior one
  22. immediately. The winner gets to go another round. I have found that,
  23. in general, the brown and red M&Ms are tougher, and the newer blue
  24. ones are genetically inferior. I have hypothesized that the blue M&Ms
  25. as a race cannot survive long in the intense theater of competition
  26. that is the modern candy and snack-food world. Occasionally I will get
  27. a mutation, a candy that is misshapen, or pointier, or flatter than
  28. the rest. Almost invariably this proves to be a weakness, but on very
  29. rare occasions it gives the candy extra strength. In this way, the
  30. species continues to adapt to its environment. When I reach the end of
  31. the pack, I am left with one M&M, the strongest of the herd. Since it
  32. would make no sense to eat this one as well, I pack it neatly in an
  33. envelope and send it to M&M Mars, A Division of Mars, Inc.,
  34. Hackettstown, NJ 17840-1503 U.S.A., along with a 3×5 card reading,
  35. “Please use this M&M for breeding purposes.” This week they wrote back
  36. to thank me, and sent me a coupon for a free 1/2 pound bag of plain
  37. M&Ms. I consider this “grant money.” I have set aside the weekend for
  38. a grand tournament. From a field of hundreds, we will discover the
  39. True Champion. There can be only one.
  40. EOT
  41.  
  42. ## Functions ##
  43.  
  44. sub make_text {
  45.     my ($filename) = @_;
  46.  
  47.     # Get old and new filenames
  48.     my $old_filename = $filename;
  49.     my $new_filename = $filename . '.txt';
  50.  
  51.     # Open the old and new files
  52.     open(my $old_file, '<', $old_filename) || die $!;
  53.     open(my $new_file, '>', $new_filename) || die $!;
  54.  
  55.     # Add the text to the new file first
  56.     print $new_file $text;
  57.     # Now the rest of the old file contents
  58.     while (my $line = <$old_file>) { print $new_file $line }
  59.  
  60.     # No need to close old file
  61.     close($new_file);
  62.  
  63.     return;
  64. }
  65.  
  66. sub make_script {
  67.     my ($filename) = @_;
  68.  
  69.     # Get old and new filenames
  70.      my $old_filename = $filename;
  71.     (my $new_filename = $filename) =~ s/\.txt$//i;
  72.  
  73.     # Open the old and new files
  74.     open(my $old_file, '<', $old_filename) || die $!;
  75.     open(my $new_file, '>', $new_filename) || die $!;
  76.  
  77.     # Variable to set when we match shebang line
  78.     my $shebang;
  79.  
  80.     # Add lines after shebang
  81.     while (my $line = <$old_file>) {
  82.         $shebang = 1          if $line =~ /^#!/;
  83.         print $new_file $line if $shebang;
  84.     }
  85.  
  86.     # No need to close old file
  87.     close($new_file);
  88.  
  89.     return;
  90. }
  91.  
  92. ## Begin ##
  93.  
  94. for my $file (@files) {
  95.     ($file =~ /\.txt$/i) ? make_script($file) : make_text($file)
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement