Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Here's my POD documentation for this module (which is basically what the latest version looks like):
=head1 NAME Filter::Handle - Apply filters to output filehandles =head1 SYNOPSIS use Filter::Handle; my $f = Filter::Handle->new(\*STDOUT); use Filter::Handle qw/subs/; Filter \*STDOUT; ... UnFilter \*STDOUT; =head1 DESCRIPTION I<Filter::Handle> allows you to apply arbitrary filters to output filehandles. You can perform any sorts of transformations on the outgoing text: you can prepend it with some data, you can replace all instances of one word with another, etc. You can even filter all of your output to one filehandle and send it to another; for example, you can filter everything written to STDOUT and write it instead to another filehandle. To do this, you need to explicitly use the I<tie> interface (see below). =head2 Calling Interfaces There are three interfaces to filtering a handle: =over 4 =item * Functional use Filter::Handle qw/subs/; Filter \*STDOUT; print "I am filtered text"; UnFilter \*STDOUT; print "I am normal text"; The functional interface works by exporting two functions into the caller's namespace: I<Filter> and I<UnFilter>. To start filtering a filehandle, call the I<Filter> function; to stop, call I<UnFilter> on that same filehandle. Any writes between the time you start and stop filtering will be filtered. =item * Object-Oriented use Filter::Handle; { my $f = Filter::Handle->new(\*STDOUT); print "I am filtered text"; } print "I am normal text"; The object-oriented interface works by filtering the filehandle while your object is in scope. Once all references to that object have gone out of scope--typically, this is after your one reference has gone away--the filehandle will no longer be filtered. =item * Tie Interface use Filter::Handle; local *HANDLE; tie *STDOUT, 'Filter::Handle', \*HANDLE; print "I am filtered text written to HANDLE"; untie *STDOUT; The I<tie> interface will filter your filehandle until you explicitly I<untie> it. This is the only interface that allows you to filter one filehandle through another. The above example will filter all writes to STDOUT through the output filter, then write it out on HANDLE. Note that this is different behavior than that of the first two interfaces; if you want your output written to the same handle that you're filtering, you could use: tie *STDOUT, 'Filter::Handle', \*STDOUT; Which is exactly what the first two interfaces do. =back =head2 Customized Filters The default filter is relatively boring: it simply prepends any text you print with the filename and line of the invoking caller. You'll probably want to do something more interesting. To do so, pass an anonymous subroutine as a second argument to either the I<new> method, if you're using the OO interface, or to the I<Filter> function, if you're using the functional interface. Your subroutine will be passed the list originally passed to print, and it should return another list, suitable for passing to your (unfiltered) output filehandle. For example, say that we want to replace all instances of "blue" with "red". We could say: use Filter::Handle qw/subs/; Filter \*STDOUT, sub { local $_ = "@_"; s/blue/red/g; $_ }; print "My house is blue.\n"; print "So is my cat, whose nose is blue.\n"; UnFilter \*STDOUT; print "And the plane is also blue.\n"; This prints: My house is red. So is my cat, whose nose is red. And the plane is also blue. As expected. =head1 CAVEATS Note that this won't work correctly with output from XSUBs or system calls. This is due to a limitation of Perl's I<tie> mechanism when tying filehandles. =head1 AUTHOR Benjamin Trott, ben@rhumba.pair.com =head1 CREDITS Thanks to tilly, chromatic, Adam, and merlyn at PerlMonks.org for suggestions, critiques, and code samples. =cut

In reply to RE: RE: Filehandle Filter by btrott
in thread Filehandle Filter by btrott

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 rifling through the Monastery: (14)
    As of 2014-08-28 16:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (264 votes), past polls