IO::Null -- class for null filehandles


  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!

Or even:

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


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.


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


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


IO::Handle, perltie, IO::Scalar


* This:

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

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.

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.

emits no warnings.

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

* This:


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 (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.


Sean M. Burke