Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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; }

In reply to Re: CUFP: Random tag insertion by Coruscate
in thread CUFP: Random tag insertion by William G. Davis

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2022-08-19 14:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found