1IO::Interactive(3) User Contributed Perl Documentation IO::Interactive(3)
2
3
4
6 IO::Interactive - Utilities for interactive I/O
7
9 This document describes IO::Interactive version 1.02
10
12 use IO::Interactive qw(is_interactive interactive busy);
13
14 if ( is_interactive() ) {
15 print "Running interactively\n";
16 }
17
18 # or...
19
20 print {interactive} "Running interactively\n";
21
22
23 $fh = busy {
24 do_noninteractive_stuff();
25 }
26
28 This module provides three utility subroutines that make it easier to
29 develop interactive applications.
30
31 The "ARGV" filehandle, the one that "<>" or an empty readline() uses,
32 has various magic associated with it. It's not actually opened until
33 you try to read from it. Checking "-t ARGV" before you've tried to read
34 from it might give you the wrong answer. Not only that, you might not
35 read from "ARGV". If the value in @ARGV is the magic filename "-" (a
36 convention to mean the standard filehandle for input or output), "ARGV"
37 might actually be "STDIN". You don't want to think about all of this.
38 This module is discussed in Perl Best Practices on page 218. Also see
39 the "ARGV" entry in perlvar and the "readline" entry in perlfunc.
40
41 is_interactive()
42 This subroutine returns true if *ARGV and the currently selected
43 filehandle (usually *STDOUT) are connected to the terminal. The
44 test is considerably more sophisticated than:
45
46 -t *ARGV && -t *STDOUT
47
48 as it takes into account the magic behaviour of *ARGV.
49
50 You can also pass "is_interactive" a writable filehandle, in which
51 case it requires that filehandle be connected to a terminal
52 (instead of the currently selected). The usual suspect here is
53 *STDERR:
54
55 if ( is_interactive(*STDERR) ) {
56 carp $warning;
57 }
58
59 Note that "is_interactive" may return true in a Windows Scheduled
60 Task. See Github #6
61 (https://github.com/briandfoy/io-interactive/issues/6).
62
63 interactive()
64 This subroutine returns *STDOUT if "is_interactive" is true. If
65 is_interactive() is false, "interactive" returns a filehandle that
66 does not print.
67
68 This makes it easy to create applications that print out only when
69 the application is interactive:
70
71 print {interactive} "Please enter a value: ";
72 my $value = <>;
73
74 You can also pass "interactive" a writable filehandle, in which
75 case it writes to that filehandle if it is connected to a terminal
76 (instead of writing to *STDOUT). Once again, the usual suspect is
77 *STDERR:
78
79 print {interactive(*STDERR)} $warning;
80
81 "busy {...}"
82 This subroutine takes a block as its single argument and executes
83 that block. Whilst the block is executed, *ARGV is temporarily
84 replaced by a closed filehandle. That is, no input from *ARGV is
85 possible in a "busy" block. Furthermore, any attempts to send
86 input into the "busy" block through *ARGV is intercepted and a
87 warning message is printed to *STDERR. The "busy" call returns a
88 filehandle that contains the intercepted input.
89
90 A "busy" block is therefore useful to prevent attempts at input
91 when the program is busy at some non-interactive task.
92
94 Unknown subroutine (%s) requested
95 This module only exports the three subroutines described above.
96 You asked for something else. Maybe you misspelled the subroutine
97 you wanted.
98
100 IO::Interactive requires no configuration files or environment
101 variables.
102
104 This module requires the openhandle() subroutine from the Scalar::Util
105 module.
106
108 None reported.
109
111 No bugs have been reported.
112
113 Please report any bugs or feature requests to Github
114 <https://github.com/briandfoy/io-interactive/issues>.
115
117 This code is in GitHub:
118
119 https://github.com/briandfoy/io-interactive
120
122 Damian Conway "<DCONWAY@cpan.org>"
123
124 Currently maintained by brian d foy "<bdfoy@cpan.org>".
125
126 1.01 patch DMUEY "dmuey@cpan.org"
127
129 Copyright © 2005-2021, Damian Conway "<DCONWAY@cpan.org>". All rights
130 reserved.
131
132 This module is free software; you can redistribute it and/or modify it
133 under the same terms as Perl itself.
134
136 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
137 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
138 WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
139 PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
140 EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
141 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
142 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
143 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
144 NECESSARY SERVICING, REPAIR, OR CORRECTION.
145
146 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
147 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
148 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
149 TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
150 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
151 SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
152 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
153 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
154 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
155 DAMAGES.
156
157
158
159perl v5.38.0 2023-07-20 IO::Interactive(3)