1Perl::Critic::Policy:P:eIUrnslpe:ur:tCOCruoitntptiurcti::b::uPPtorelodihciPybe:ir:tlITnwDpoouActruOgmuOetpnpetunat(t:3i:poPmnr)ohibitTwoArgOpen(3pm)
2
3
4
6 Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen - Write "open
7 $fh, q{<}, $filename;" instead of "open $fh, "<$filename";".
8
10 This Policy is part of the core Perl::Critic distribution.
11
13 The three-argument form of "open" (introduced in Perl 5.6) prevents
14 subtle bugs that occur when the filename starts with funny characters
15 like '>' or '<'. The IO::File module provides a nice object-oriented
16 interface to filehandles, which I think is more elegant anyway.
17
18 open( $fh, '>output.txt' ); # not ok
19 open( $fh, q{>}, 'output.txt' ); # ok
20
21 use IO::File;
22 my $fh = IO::File->new( 'output.txt', q{>} ); # even better!
23
24 It's also more explicitly clear to define the input mode of the file,
25 as in the difference between these two:
26
27 open( $fh, 'foo.txt' ); # BAD: Reader must think what default mode is
28 open( $fh, '<', 'foo.txt' ); # GOOD: Reader can see open mode
29
30 There is also a one-argument form of "open" which retrieves the
31 expression to open from the global variable with the same name as the
32 handle, but this has the same problems as the two-argument form, and
33 adds in more ambiguity.
34
35 our $FH = '<foo.txt';
36 open( FH ); # not ok
37
38 This policy will not complain if the file explicitly states that it is
39 compatible with a version of perl prior to 5.6 via an include
40 statement, e.g. by having "require 5.005" in it.
41
43 This Policy is not configurable except for the standard options.
44
46 There is one case in which you are forced to use the two-argument form
47 of open: when doing a safe pipe open, as described in perlipc.
48
50 IO::Handle
51
52 IO::File
53
55 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
56
58 Copyright (c) 2005-2021 Imaginative Software Systems. All rights
59 reserved.
60
61 This program is free software; you can redistribute it and/or modify it
62 under the same terms as Perl itself.
63
64
65
66perl v5.38.0 Perl::Critic::2P0o2l3i-c0y9:-:2I5nputOutput::ProhibitTwoArgOpen(3pm)