| Start | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Opening A FileHandle for Reading Opening Generic ASCII Text FilesThis is the basic idiom for "default" File IO and makes Perl itself does not handle errors in file opening, so you have to handle those yourself by checking the exit condition of On Windows, the default decoder is a "CRLF" filter, which maps any "\r\n" sequences in the input to "\n" Opening Binary FilesThis specifies that Perl should not perform a Opening UTF8 Text FilesThis specifies that Perl should both avoid |
||||||||||||||
Reading from a file If you know that your input file is UTF-8, you can specify the encoding: After finished reading from the file, the filehandle should be closed: See also: Reading a file into a variable (opens new window) Another and faster way to read a file is to use File::Slurper Module. This is useful if you work with many files. |
||||||||||||||
| Write to a file This code opens a file for writing. Returns an error if the file couldn't be opened. Also closes the file at the end. Now we have an open file ready for writing which we access through The To write the data into the file, the |
||||||||||||||
"use autodie" and you won't need to check file open/close failures |
||||||||||||||
| Rewind a filehandle Sometimes it is needful to backtrack after reading. |
||||||||||||||
Reading and Writing gzip compressed files Writing a gzipped fileTo write a gzipped file, Reading from a gzipped fileTo read from a gzipped file, |
||||||||||||||
Setting the default Encoding for IO This ASCII is a subset of UTF-8, so this is not expected to cause any problems with legacy ASCII files and will help protect you the accidental file corruption that can happen when treating UTF-8 files as ASCII. However, it is important that you know what the encoding of your files is that you are dealing with and handle them accordingly. (Reasons that we should not ignore Unicode. (opens new window)) For more in depth treatment of Unicode, please see the Perl Unicode topic (opens new window). |
||||||||||||||
| Reading from and writing to a file Before reading and writing text files you should know what encoding to use. See the Perl Unicode Documentation for more details on encoding (opens new window). Here we show the setting of UTF-8 as the default encoding and decoding for the function The
Reading Writing Reading chunks Opening and reading big files can take some time and resources. If only a small part of the content is required, it might be a good idea to read the content in chunks using the
Reads 16 characters from the beginning of the file into Parameters
Remarks
Beware of the difference between characters and bytes: Not all encodings - especially UTF-8 - use 1-byte-characters. While this is handled pretty much flawlessly by PerlIO, there is one potential pitfall of note:
So don't use arithmetics based on these mixed values. Instead use e.g. |
Read Article
https://develop.horje.com/learn/1434/reference