IO::Null(3)

πŸ“¦
version 1.01 Β· 2 versions
, in fedora 43

✨ IO::Null πŸ’–

okay bestie, this is IO::Null β€” a class for null filehandles, straight out of the User Contributed Perl Documentation. it's short, it's silly, and it slays. πŸŽ€


🌸 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 πŸ’«

okay so this is a class for null filehandles. that's the whole vibe. πŸ¦‹

calling a constructor of this class always succeeds, and hands you back a fresh new null filehandle. no drama, no failure, it just works.

writing to any object of this class is always a no-operation, and it returns true. you're basically screaming into the void and the void is like "yeah sure πŸ’…".

reading from any object of this class is always no-operation too, and it returns empty-string or empty-list, as appropriate. depends on what you asked for, bestie.


🌈 WHY 🌈

you could say:

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

and get a null FH that way. sure. but not everyone is using an OS that has a /dev/null. 😭 so like... not the move for everybody.


πŸ‘‘ IMPLEMENTATION πŸ‘‘

this is a subclass of IO::Handle. applicable methods get subs that do nothing, and return an appropriate value. that's it. that's the whole thing. iconic minimalism, fr fr.


🧚 SEE ALSO 🧚

IO::Handle, perltie, IO::Scalar


πŸ’€ CAVEATS πŸ’€

okay this is the part where things get weird and i need you to stay with me.

  • 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 the author has no idea why. literally no idea. 😭

  • 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. and the author would 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. ✨ slay.

  • the author doesn't know if you can successfully untaint a null filehandle. unknown. mystery. leaving it there. πŸ™ˆ
  • 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! no cap.

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"


and that's the whole page, bestie!! this was pulled from perl v5.42.0, dated 2025-07-25, and it's the IO::Null(3) manual page. go write to a void and feel zero consequences. πŸ¦‹βœ¨πŸ’–

πŸ“¦ where this page comes from