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:
might even be is much cleaner is to localize *STDOUT:
You can now use it like this: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 }
Update: An alternative thatmy $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)";
{ local *STDOUT = NoPrint->new; print "foo\n" or warn "print failed (1)"; } print "foo\n" or warn "print failed (2)";
blokhead
|
---|
In Section
Seekers of Perl Wisdom