#!c:/perl/bin/perl -w $|++; use strict; use Getopt::Std; sub show_usage; my $text; show_usage unless getopts('t:i:o:h', \my %opts); show_usage if $opts{'h'} or not $opts{'t'}; # read input file if ($opts{'i'}) { open my $fh, '<', $opts{'i'} or die "Couldn't open input file for reading: $!\n"; $text = do { local $/; <$fh> }; close $fh or die "Coudn't close input file: $!\n"; } # get text from console else { print "Ready for Input:\n\n"; $text = ; } my $t_open = '<' . $opts{'t'} . '>'; my $t_close = ''; # insert random tags $text = join '', map { /\s/ || int rand 2 ? $_ : $t_open . $_ . $t_close } split //, $text; $text =~ s/\Q$t_close$t_open\E//g; # output to outfile if (defined $opts{'o'}) { open my $fh, '>', $opts{'o'} or die "Couldn't open output file for writing: $!\n"; print $fh $text; close $fh or die "Couldn't close output file: $!\n"; print "Text saved to output file $opts{'o'}.\n"; } # make backup/save to infile elsif (defined $opts{'i'}) { rename $opts{'i'}, $opts{'i'} . '.bak'; open my $fh, '>', $opts{'i'} or die "Couldn't open output file for writing: $!\n"; print $fh $text; close $fh or die "Couldn't close output file: $!\n"; print "Input file $opts{'i'} backed up to $opts{'i'}.bak.\n", "Text saved back to output file $opts{'i'}.\n"; } # print to console else { print $text, "\n"; } # display usage information sub show_usage { print <<"END_OF_USAGE"; This script randomly inserts a specified HTML tag into some text. Usage: \$ $0 -t htmltag [-i] [-o] [-h] Options: -t The html tag you wish to insert (ie: "em", "strong"). -i (Optional) The input file from which to read the text into which the random html tags will be inserted. -o (Optional) The file to which the generated output will be printed. If you do not supply this option, the input file will be used for output. A backup of the original input file will first be made. If an input file was not used, output will be printed to the console. Flags: -h Displays this help message. END_OF_USAGE exit; }