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

A Strange print() predicament.

by Anonymous Monk
on Feb 19, 2003 at 21:37 UTC ( #236827=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to require a file, and not print() when it tells me to do so.
Simply something like { my $variable_for_file = 'moose'; # Somehow stop printing. do; # Nothing should be printed. } #Now print calls should work again.

Replies are listed 'Best First'.
Re: A Strange print() predicament.
by Zaxo (Archbishop) on Feb 20, 2003 at 01:19 UTC

    Here's a real direct way to do it:

    { my $variable_for_file = 'moose'; local *CORE::GLOBAL::print = sub {1}; local *CORE::GLOBAL::printf = sub {1}; do; }
    That temporarily overrides perl's builtin print and printf with a no-op.

    Update: This doesn't work - peer into the future and see !Overriding Builtin print.

    After Compline,

Re: A Strange print() predicament.
by jasonk (Parson) on Feb 19, 2003 at 21:41 UTC

    Making a copy of STDOUT that redirects the text that gets printed is one way, though there are probably better solutions than this...

    #!/usr/bin/perl -w use strict; print "Print this\n"; { local *STDOUT; open(STDOUT,">/dev/null"); # don't allow this include file to print anything do ""; close(STDOUT) } print "Print this too\n";
Re: A Strange print() predicament.
by BigLug (Chaplain) on Feb 19, 2003 at 22:44 UTC
    Previous solutions have suggested redirecting STDOUT, which would work, but consider using the built in one-argument select statement:
    $oldFH = select(NOTHING); do "filename.txt"; print "Won't Print\n"; select($oldFH); print "Will Print\n";
    The select statement will change the default filehandle for all manner of things, including $| and print. It also means that if a called module does something like:
    print STDOUT "This will print";
    ... then it will print. However I'd be surprised if any module actually does this. It also gives you an 'out' if you want to force something to print while you have the select redirected.
Re: A Strange print() predicament.
by steves (Curate) on Feb 19, 2003 at 21:47 UTC

    Or (same idea different way):

    use strict; use IO::Null; print "Printing\n"; { local *STDOUT = IO::Null->new(); print "Not printing\n"; } print "Printing again\n";
Re: A Strange print() predicament.
by Tomte (Priest) on Feb 19, 2003 at 21:44 UTC

    perldoc -f open and search for redirect...

    in short using a un*x, but read the above mentioned part for the complete story:

    open STDOUT, '>', "/dev/null"; open STDERR, '>&STDOUT';


Re: A Strange print() predicament.
by waswas-fng (Curate) on Feb 19, 2003 at 21:57 UTC
    if you want to stop the file you are use or require from outputing to stdout/stderr you can do what these people are saying or open the file suck in the lines regex out or comment out any lines that have print in them then evel the lines. Redirecting stdout/err is the cleanest way I think.

Re: A Strange print() predicament.
by cfreak (Chaplain) on Feb 20, 2003 at 14:39 UTC

    While several people have given you good answers for what you are trying to do, maybe you should examine why you are trying to do it.

    IMO (so take it for what its worth) this seems like a nasty hack that could bite you or someone else down the road when you try to maintain this code. Its much better to write your modules in such away that they simply return what you want rather than outputting something to STDOUT. Also, while using do in this manner is not depreciated perldoc clearly states that using use or require is generally a better idea for importing code.

    Finally, nitpick: Perl modules most often end in a '.pm' or '.pl' extension, a good idea because then anyone else who might maintain your code in the future will know that it is a perl module. (Sorry my hatred of PHP gets the better of me from time to time :) )


    Lobster Aliens Are attacking the world!
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://236827]
Approved by traveler
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2023-03-31 18:37 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (76 votes). Check out past polls.