Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (6)
    As of 2021-04-19 11:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?