Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- # Convert scripts to text files (or back to scripts)
- # Daniel Bowling <dbowling@akamai.com>
- # Jan 2021
- use strict;
- use warnings;
- use utf8;
- use open qw{:std :utf8};
- use Data::Dumper; # Uncomment for debugging
- ## Variables ##
- my @files = @ARGV;
- my $text = <<'EOT';
- Whenever I get a package of plain M&Ms, I make it my duty to continue
- the strength and robustness of the candy as a species. To this end, I
- hold M&M duels. Taking two candies between my thumb and forefinger, I
- apply pressure, squeezing them together until one of them cracks and
- splinters. That is the “loser,” and I eat the inferior one
- immediately. The winner gets to go another round. I have found that,
- in general, the brown and red M&Ms are tougher, and the newer blue
- ones are genetically inferior. I have hypothesized that the blue M&Ms
- as a race cannot survive long in the intense theater of competition
- that is the modern candy and snack-food world. Occasionally I will get
- a mutation, a candy that is misshapen, or pointier, or flatter than
- the rest. Almost invariably this proves to be a weakness, but on very
- rare occasions it gives the candy extra strength. In this way, the
- species continues to adapt to its environment. When I reach the end of
- the pack, I am left with one M&M, the strongest of the herd. Since it
- would make no sense to eat this one as well, I pack it neatly in an
- envelope and send it to M&M Mars, A Division of Mars, Inc.,
- Hackettstown, NJ 17840-1503 U.S.A., along with a 3×5 card reading,
- “Please use this M&M for breeding purposes.” This week they wrote back
- to thank me, and sent me a coupon for a free 1/2 pound bag of plain
- M&Ms. I consider this “grant money.” I have set aside the weekend for
- a grand tournament. From a field of hundreds, we will discover the
- True Champion. There can be only one.
- EOT
- ## Functions ##
- sub make_text {
- my ($filename) = @_;
- # Get old and new filenames
- my $old_filename = $filename;
- my $new_filename = $filename . '.txt';
- # Open the old and new files
- open(my $old_file, '<', $old_filename) || die $!;
- open(my $new_file, '>', $new_filename) || die $!;
- # Add the text to the new file first
- print $new_file $text;
- # Now the rest of the old file contents
- while (my $line = <$old_file>) { print $new_file $line }
- # No need to close old file
- close($new_file);
- return;
- }
- sub make_script {
- my ($filename) = @_;
- # Get old and new filenames
- my $old_filename = $filename;
- (my $new_filename = $filename) =~ s/\.txt$//i;
- # Open the old and new files
- open(my $old_file, '<', $old_filename) || die $!;
- open(my $new_file, '>', $new_filename) || die $!;
- # Variable to set when we match shebang line
- my $shebang;
- # Add lines after shebang
- while (my $line = <$old_file>) {
- $shebang = 1 if $line =~ /^#!/;
- print $new_file $line if $shebang;
- }
- # No need to close old file
- close($new_file);
- return;
- }
- ## Begin ##
- for my $file (@files) {
- ($file =~ /\.txt$/i) ? make_script($file) : make_text($file)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement