Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Testing failures: How to override print to make it fail?

by blokhead (Monsignor)
on May 05, 2005 at 03:43 UTC ( #454234=note: print w/replies, xml ) Need Help??


in reply to Testing failures: How to override print to make it fail?

You can force print to fail by selecting a filehandle that you know is unwritable. Here's a quick-n-dirty little module that returns a tied filehandle that fails when written to:
package NoPrint; use Symbol; sub new { my $sym = gensym; tie *$sym, $_[0]; bless $sym, $_[0]; } sub TIEHANDLE { bless {}, $_[0] } sub PRINT { return undef } sub PRINTF { return undef } sub WRITE { return undef }
You can now use it like this:
my $old_fh = select NoPrint->new; ## deep inside some test somewhere... print "foo\n" or warn "print failed (1)"; select $old_fh; ## now printing is back to normal again: print "foo\n" or warn "print failed (2)";
Update: An alternative that might even be is much cleaner is to localize *STDOUT:
{ local *STDOUT = NoPrint->new; print "foo\n" or warn "print failed (1)"; } print "foo\n" or warn "print failed (2)";

blokhead

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://454234]
help
Chatterbox?
[davido]: The code seems pretty sane, and relatively foolproof. I guess I can answer my own question; move() should work across filesystems so long as they're mounted, rw, and not filled up.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2016-12-10 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (159 votes). Check out past polls.