1IO::Null(3) User Contributed Perl Documentation IO::Null(3)
2
3
4
6 IO::Null -- class for null filehandles
7
9 use IO::Null;
10 my $fh = IO::Null->new;
11 print $fh "I have nothing to say\n"; # does nothing.
12 # or:
13 $fh->print("And I'm saying it.\n"); # ditto.
14 # or:
15 my $old = select($fh);
16 print "and that is poetry / as I needed it --John Cage"; # nada!
17 select($old);
18
19 Or even:
20
21 tie(*FOO, IO::Null);
22 print FOO "Lalalalala!\n"; # does nothing.
23
25 This is a class for null filehandles.
26
27 Calling a constructor of this class always succeeds, returning a new
28 null filehandle.
29
30 Writing to any object of this class is always a no-operation, and
31 returns true.
32
33 Reading from any object of this class is always no-operation, and
34 returns empty-string or empty-list, as appropriate.
35
37 You could say:
38
39 open(NULL, '>/dev/null') || die "WHAAT?! $!";
40
41 and get a null FH that way. But not everyone is using an OS that has a
42 "/dev/null"
43
45 This is a subclass of IO::Handle. Applicable methods with subs that do
46 nothing, and return an appropriate value.
47
49 IO::Handle, perltie, IO::Scalar
50
52 * This:
53
54 use IO::Null;
55 $^W = 1; # turn on warnings
56 tie(*FOO, IO::Null);
57 print FOO "Lalalalala!\n"; # does nothing.
58 untie(*FOO);
59
60 has been known to produce this odd warning:
61
62 untie attempted while 3 inner references still exist.
63
64 and I've no idea why.
65
66 * Furthermore, this:
67
68 use IO::Null;
69 $^W = 1;
70 *FOO = IO::Null->new;
71 print FOO "Lalalalala!\n"; # does nothing.
72 close(FOO);
73
74 emits these warnings:
75
76 Filehandle main::FOO never opened.
77 Close on unopened file <GLOB>.
78
79 ...which are, in fact, true; the FH behind the FOO{IO} was never opened
80 on any real filehandle. (I'd welcome anyone's (working) suggestions on
81 how to suppress these warnings.)
82
83 You get the same warnings with:
84
85 use IO::Null;
86 $^W = 1;
87 my $fh = IO::Null->new;
88 print $fh "Lalalalala!\n"; # does nothing.
89 close $fh;
90
91 Note that this, however:
92
93 use IO::Null;
94 $^W = 1;
95 my $fh = IO::Null->new;
96 $fh->print("Lalalalala!\n"); # does nothing.
97 $fh->close();
98
99 emits no warnings.
100
101 * I don't know if you can successfully untaint a null filehandle.
102
103 * This:
104
105 $null_fh->fileno
106
107 will return a defined and nonzero number, but one you're not likely to
108 want to use for anything. See the source.
109
110 * These docs are longer than the source itself. Read the source!
111
113 Copyright (c) 2000 Sean M. Burke. All rights reserved.
114
115 This library is free software; you can redistribute it and/or modify it
116 under the same terms as Perl itself.
117
119 Sean M. Burke "sburke@cpan.org"
120
121
122
123perl v5.28.0 2004-12-30 IO::Null(3)