File Coverage

blib/lib/IO/ExplicitHandle.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             IO::ExplicitHandle - force I/O handles to be explicitly specified
4              
5             =head1 SYNOPSIS
6              
7             use IO::ExplicitHandle;
8              
9             no IO::ExplicitHandle;
10              
11             =head1 DESCRIPTION
12              
13             This module provides a lexically-scoped pragma that prohibits I/O
14             operations that implicitly default to an I/O handle determined at
15             runtime. For example, C
16             123> implicitly uses the "currently selected" I/O handle (controlled
17             by L). Within the context of the pragma, I/O
18             operations must be explicitly told which handle they are to operate on.
19             For example, C explicitly uses the program's standard
20             output stream.
21              
22             The affected operations are those that use either the "currently selected"
23             I/O handle or the "last read" I/O handle.
24             The affected operations that use the "currently selected" I/O handle are
25             L, L, L,
26             L, L, and the magic variables
27             L<$E|perlvar/$E>, L<$^|perlvar/$^>, L<$~|perlvar/$~>,
28             L<$=|perlvar/$=>, L<$-|perlvar/$->, and L<$%|perlvar/$%>. The affected
29             operations that use the "last read" I/O handle are L,
30             L, and the magic variable L<$.|perlvar/$.>.
31              
32             One form
33             of the L<..|perlop/..> operator can implicitly read L<$.|perlvar/$.>,
34             but it cannot be reliably distinguished at compile time from the more
35             common list-generating form, so it is not affected by this module.
36              
37             The L function returns the "currently
38             selected" I/O handle, and similarly the magic variable
39             L<${^LAST_FH}|perlvar/${^LAST_FH}> refers to the "last read" I/O handle.
40             Such explicit retrieval of the I/O handles to which some operations
41             default isn't itself considered an operation on the handle, and so is
42             not affected by this module.
43              
44             The L function when called without
45             arguments, and its syntactic sugar alias C<< <> >>, default to the
46             C I/O handle. Because this is a fixed default, rather than using
47             a hidden runtime variable, it is considered explicit enough, and so is
48             not affected by this module. Relatedly, when the L
49             function is called with an empty parenthesised argument list (as opposed
50             to calling it with no parentheses), it performs a unique operation
51             which is concerned with the C I/O handle but is not the same
52             as C. This operation doesn't amount to defaulting an I/O
53             handle argument at all, and is also not affected by this module.
54             Likewise, the C<<< <<>> >>> operator performs a unique operation on the
55             C handle, and is also not affected by this module.
56              
57             =cut
58              
59             package IO::ExplicitHandle;
60              
61 2     2   58317 { use 5.006; }
  2         15  
62 2     2   934 use Lexical::SealRequireHints 0.012;
  2         2869  
  2         10  
63 2     2   58 use warnings;
  2         4  
  2         36  
64 2     2   8 use strict;
  2         4  
  2         108  
65              
66             our $VERSION = "0.002";
67              
68             require XSLoader;
69             XSLoader::load(__PACKAGE__, $VERSION);
70              
71             =head1 PACKAGE METHODS
72              
73             =over
74              
75             =item IO::ExplicitHandle->import
76              
77             Turns on the I/O handle stricture in the lexical environment that is
78             currently compiling.
79              
80             =item IO::ExplicitHandle->unimport
81              
82             Turns off the I/O handle stricture in the lexical environment that is
83             currently compiling.
84              
85             =back
86              
87             =head1 BUGS
88              
89             The L<..|perlop/..> operator decides only at runtime whether it will
90             read from L<$.|perlvar/$.>, and hence implicitly use the "last read"
91             I/O handle. It does this if called in scalar context. If the same
92             expression is called in list context, it generates a list of numbers,
93             unrelated to L<$.|perlvar/$.>. This semantic overloading prevents the
94             problematic use of L<..|perlop/..> being detected at compile time.
95              
96             =head1 SEE ALSO
97              
98             L
99              
100             =head1 AUTHOR
101              
102             Andrew Main (Zefram)
103              
104             =head1 COPYRIGHT
105              
106             Copyright (C) 2012, 2017, 2023 Andrew Main (Zefram)
107              
108             =head1 LICENSE
109              
110             This module is free software; you can redistribute it and/or modify it
111             under the same terms as Perl itself.
112              
113             =cut
114              
115             1;