1FileHandle(3pm) Perl Programmers Reference Guide FileHandle(3pm)
2
3
4
6 FileHandle - supply object methods for filehandles
7
9 use FileHandle;
10
11 $fh = FileHandle->new;
12 if ($fh->open("< file")) {
13 print <$fh>;
14 $fh->close;
15 }
16
17 $fh = FileHandle->new("> FOO");
18 if (defined $fh) {
19 print $fh "bar\n";
20 $fh->close;
21 }
22
23 $fh = FileHandle->new("file", "r");
24 if (defined $fh) {
25 print <$fh>;
26 undef $fh; # automatically closes the file
27 }
28
29 $fh = FileHandle->new("file", O_WRONLY|O_APPEND);
30 if (defined $fh) {
31 print $fh "corge\n";
32 undef $fh; # automatically closes the file
33 }
34
35 $pos = $fh->getpos;
36 $fh->setpos($pos);
37
38 $fh->setvbuf($buffer_var, _IOLBF, 1024);
39
40 ($readfh, $writefh) = FileHandle::pipe;
41
42 autoflush STDOUT 1;
43
45 NOTE: This class is now a front-end to the IO::* classes.
46
47 "FileHandle::new" creates a "FileHandle", which is a reference to a
48 newly created symbol (see the "Symbol" package). If it receives any
49 parameters, they are passed to "FileHandle::open"; if the open fails,
50 the "FileHandle" object is destroyed. Otherwise, it is returned to the
51 caller.
52
53 "FileHandle::new_from_fd" creates a "FileHandle" like "new" does. It
54 requires two parameters, which are passed to "FileHandle::fdopen"; if
55 the fdopen fails, the "FileHandle" object is destroyed. Otherwise, it
56 is returned to the caller.
57
58 "FileHandle::open" accepts one parameter or two. With one parameter,
59 it is just a front end for the built-in "open" function. With two
60 parameters, the first parameter is a filename that may include
61 whitespace or other special characters, and the second parameter is the
62 open mode, optionally followed by a file permission value.
63
64 If "FileHandle::open" receives a Perl mode string (">", "+<", etc.) or
65 a POSIX fopen() mode string ("w", "r+", etc.), it uses the basic Perl
66 "open" operator.
67
68 If "FileHandle::open" is given a numeric mode, it passes that mode and
69 the optional permissions value to the Perl "sysopen" operator. For
70 convenience, "FileHandle::import" tries to import the O_XXX constants
71 from the Fcntl module. If dynamic loading is not available, this may
72 fail, but the rest of FileHandle will still work.
73
74 "FileHandle::fdopen" is like "open" except that its first parameter is
75 not a filename but rather a file handle name, a FileHandle object, or a
76 file descriptor number.
77
78 If the C functions fgetpos() and fsetpos() are available, then
79 "FileHandle::getpos" returns an opaque value that represents the
80 current position of the FileHandle, and "FileHandle::setpos" uses that
81 value to return to a previously visited position.
82
83 If the C function setvbuf() is available, then "FileHandle::setvbuf"
84 sets the buffering policy for the FileHandle. The calling sequence for
85 the Perl function is the same as its C counterpart, including the
86 macros "_IOFBF", "_IOLBF", and "_IONBF", except that the buffer
87 parameter specifies a scalar variable to use as a buffer. WARNING: A
88 variable used as a buffer by "FileHandle::setvbuf" must not be modified
89 in any way until the FileHandle is closed or until
90 "FileHandle::setvbuf" is called again, or memory corruption may result!
91
92 See perlfunc for complete descriptions of each of the following
93 supported "FileHandle" methods, which are just front ends for the
94 corresponding built-in functions:
95
96 close
97 fileno
98 getc
99 gets
100 eof
101 clearerr
102 seek
103 tell
104
105 See perlvar for complete descriptions of each of the following
106 supported "FileHandle" methods:
107
108 autoflush
109 output_field_separator
110 output_record_separator
111 input_record_separator
112 input_line_number
113 format_page_number
114 format_lines_per_page
115 format_lines_left
116 format_name
117 format_top_name
118 format_line_break_characters
119 format_formfeed
120
121 Furthermore, for doing normal I/O you might need these:
122
123 $fh->print
124 See "print" in perlfunc.
125
126 $fh->printf
127 See "printf" in perlfunc.
128
129 $fh->getline
130 This works like <$fh> described in "I/O Operators" in perlop except
131 that it's more readable and can be safely called in a list context
132 but still returns just one line.
133
134 $fh->getlines
135 This works like <$fh> when called in a list context to read all the
136 remaining lines in a file, except that it's more readable. It will
137 also croak() if accidentally called in a scalar context.
138
139 There are many other functions available since FileHandle is descended
140 from IO::File, IO::Seekable, and IO::Handle. Please see those
141 respective pages for documentation on more functions.
142
144 The IO extension, perlfunc, "I/O Operators" in perlop.
145
146
147
148perl v5.34.1 2022-03-15 FileHandle(3pm)