Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: CUFP: Random tag insertion

by Coruscate (Sexton)
on Nov 21, 2003 at 01:03 UTC ( #308774=note: print w/replies, xml ) Need Help??

in reply to CUFP: Random tag insertion

Interesting (as long as you're not planning on using this for your next website template). I enjoy rewriting other people's code, it passes some time and I usually end up learning something new. So here's my version (not that you'd want to use this on extremely large files. I imagine this would be very inefficient as I handle the whole thing in memory):

#!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 = <STDIN>; } my $t_open = '<' . $opts{'t'} . '>'; my $t_close = '</' . $opts{'t'} . '>'; # 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 pri +nted. 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 mad +e. If an input file was not used, output will be printed to the +console. Flags: -h Displays this help message. END_OF_USAGE exit; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://308774]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2022-09-28 13:00 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (124 votes). Check out past polls.