Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Your idea of "auto-redirect"ing a filehandle to a subroutine is possible using a tie'd filehandle (see Tying FileHandles). This is not a sane solution for a long-term approach, but for the short term it will work:

package GreppedFileHandle; use strict; sub TIEHANDLE { local *FH; open my $self, \*FH; bless $self, shift } sub PRINT { my $self = shift; my $output = "@_"; if( $output =~ /\Q$::GREP_STR\E/ ) { print( STDOUT $output ); } else { print "Swallowed a line\n"; }; } package main; our $GREP_STR = '123'; # needs to be 'our', not 'my' tie *grep_fh, 'GreppedFileHandle'; print STDOUT "Header - always print this\n"; print grep_fh "My line with 123\n"; ## printed at the terminal print grep_fh "My line with 456\n"; ## not printed

For the long term approach, you should look at a logger framework (like Log::Log4perl) or a "simple" logging subroutine like your &myprint() to centralize the logging.


In reply to Re: Preprocessing print statements by Corion
in thread Preprocessing print statements by mauroid

Title:
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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2024-04-18 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found