NAME

IO::Null -- class for null filehandles

SYNOPSIS

use IO::Null;
my $fh = IO::Null->new;
print $fh "I have nothing to say\n";  # does nothing.
# or:
$fh->print("And I'm saying it.\n");   # ditto.
# or:
my $old = select($fh);
print "and that is poetry / as I needed it --John Cage"; # nada!
select($old); 

Or even:

tie(*FOO, IO::Null);
print FOO "Lalalalala!\n";  # does nothing.

DESCRIPTION

This is a class for null filehandles.

Calling a constructor of this class always succeeds, returning a new null filehandle.

Writing to any object of this class is always a no-operation, and returns true.

Reading from any object of this class is always no-operation, and returns empty-string or empty-list, as appropriate.

WHY

You could say:

open(NULL, '>/dev/null') || die "WHAAT?! $!";

and get a null FH that way. But not everyone is using an OS that has a /dev/null

IMPLEMENTATION

This is a subclass of IO::Handle. Applicable methods with subs that do nothing, and return an appropriate value.

SEE ALSO

IO::Handle, perltie, IO::Scalar

CAVEATS

* This:

use IO::Null;
$^W = 1;  # turn on warnings
tie(*FOO, IO::Null);
print FOO "Lalalalala!\n";  # does nothing.
untie(*FOO);

has been known to produce this odd warning:

untie attempted while 3 inner references still exist.

and I've no idea why.

* Furthermore, this:

use IO::Null;
$^W = 1;
*FOO = IO::Null->new;
print FOO "Lalalalala!\n";  # does nothing.
close(FOO);

emits these warnings:

Filehandle main::FOO never opened.
Close on unopened file <GLOB>.

...which are, in fact, true; the FH behind the FOO{IO} was never opened on any real filehandle. (I'd welcome anyone's (working) suggestions on how to suppress these warnings.)

You get the same warnings with:

use IO::Null;
$^W = 1;
my $fh = IO::Null->new;
print $fh "Lalalalala!\n";  # does nothing.
close $fh;

Note that this, however:

use IO::Null;
$^W = 1;
my $fh = IO::Null->new;
$fh->print("Lalalalala!\n");  # does nothing.
$fh->close();

emits no warnings.

* I don't know if you can successfully untaint a null filehandle.

* This:

$null_fh->fileno

will return a defined and nonzero number, but one you're not likely to want to use for anything. See the source.

* These docs are longer than the source itself. Read the source!

COPYRIGHT

Copyright (c) 2000 Sean M. Burke. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Sean M. Burke sburke@cpan.org