more useful options | |
PerlMonks |
Preprocessing print statementsby mauroid (Initiate) |
on Nov 11, 2020 at 10:26 UTC ( [id://11123579]=perlquestion: print w/replies, xml ) | Need Help?? |
mauroid has asked for the wisdom of the Perl Monks concerning the following question: I have a long script that uses print (to STDOUT) in various places to print data to a terminal. Now I need to add a 'grep' option within the script so that only lines that match a certain regular expression are printed; all the rest are not printed. I would be OK with adding a new FILEHANDLE in front of every print statement, but I would want to avoid calling my own print subroutine every time I want to do a print. So something like this would be OK (only the first line should be printed in STDOUT, the second would not be printed):
but not this
Ideally I would also want the ability to print lines that override the 'grep' and are printed anyway, but that's optional. So something like this:
I thought that by using 'select' I could auto-redirect the STDOUT to maybe a subroutine, and then decide there if I want to print something or not, but it seems like 'select' only redirects to an actual FILEHANDLE and can't redirect to a subroutine.
Back to
Seekers of Perl Wisdom
|
|