use Paranoid::IO::Line; PIOMAXLNSIZE = 4096; $nlines = sip($filename, @lines); $nlines = sip($filename, @lines, 1); $nlines = tailf($filename, @lines); $nlines = tailf($filename, @lines, 1); $nlines = tailf($filename, @lines, 1, -100); piolClose($filename); $nlines = slurp($filename, @lines); # Non-locking variants $nlines = nlsip($filename, @lines); $nlines = nltailf($filename, @lines); $nlines = nlslurp($filename, @lines);
Even with the paranoid slant of these functions they should really be treated as convenience functions which can simplify higher level code without incurring any significant risk to the developer or system. They inherit not only opportunistic I/O but platform-agnostic record separators via internal use of pchomp from Paranoid::Input.
NOTE: while this does build off the foundation provided by Paranoid::IO it is important to note that you should not work on the same files using :<Paranoid::IO>'s functions while also using the functions in this module. While the former works from raw I/O the latter has to manage buffers in order to identify record boundaries. If you were to, say, sip from a file, then pread or pseek elsewhere it would render those buffers not only useless, but corrupt. This is important to note since the functions here do leverage the file handle caching features provided by popen.
It should also be noted that since we're anticipating line-based records we expect every line, even the last line in a file, to be properly terminated with a record separator (new line sequence).
As with all Paranoid modules string descriptions of errors can be retrieved from Paranoid::ERROR as they occur.
sip nlsip tailf nltailf slurp nlslurp piolClose
The following specialized import lists also exist:
List Members -------------------------------------------------------- all @defaults PIOMAXLNSIZE
$nlines = sip($filename, @lines); $nlines = sip($filename, @lines, 1);
This function allows you to read a text file into memory in chunks, the lines of which are placed into the passed array reference. The chunks are read in at up to PIOMAXFSIZE in size at a time. File locking is used and autochomping is also supported.
This returns the number of lines extracted or boolean false if any errors occurred, such as lines exceeding PIOMAXLNSIZE or other I/O errors. If there were no errors but also no content it will return 0 but true, which will satisfy boolean tests.
The passed array is always purged prior to execution. This can potentially help differentiate types of errors:
$nlines = sip($filename, @lines); warn "successfully extracted lines" if $nlines and scalar @lines; warn "no errors, but no lines" if $nlines and ! scalar @lines; warn "line length exceeded on some lines" if !$nlines and scalar @lines; warn "I/O errors or all lines exceeded line length" if !$nlines and ! scalar @lines;
Typically, if all one cares about is extracting good lines and discarding bad ones all you need is:
warn "good to go" if scalar @lines or $nlines; # or, more likely: if (@lines) { # process input... }
NOTE: sip does try to check the file stat with every call. This allows us to automatically flush buffers and reopen files in the event that the file you're sipping from was truncated, deleted, or overwritten.
The third argument is a boolean option which controls whether lines are automatically chomped or not. It defaults to not.
$nlines = nlsip($filename, @lines); $nlines = nlsip($filename, @lines, 1);
A very thin wrapper for sip that disables file locking.
$nlines = tailf($filename, @lines); $nlines = tailf($filename, @lines, 1); $nlines = tailf($filename, @lines, 1, -100);
The only difference between this function and sip is that tailf opens the file and immediately seeks to the end. If an optional fourth argument is passed it will seek backwards to extract and return that number of lines (if possible). Depending on the number passed one must be prepared for enough memory to be allocated to store PIOMAXLNSIZE * that number. If no number is specified it is assumed to be -10. Specifying this argument on a file already opened by sip or tailf will have no effect.
Return values are identical to sip.
$nlines = nltailf($filename, @lines); $nlines = nltailf($filename, @lines, -100); $nlines = nltailf($filename, @lines, -100, 1);
A very thin wrapper for tailf that disables file locking.
$nlines = slurp($filename, @lines); $nlines = slurp($filename, @lines, 1);
This function is essentially another wrapper for sip, but with some different behavior. While sip was written from the expectation that the developer would be either working on chunks from a very large file or a file that may grow while being accessed. slurp, on the other hand, expects to work exclusively on small files that can safely fit into memory. It also sees no need to cache file handles since all operations will subsequently be done in memory.
Files with slurp are explicitly closed after the read. All the normal safeguards apply: PIOMAXFSIZE is the largest amount of data that will be read into memory, and all lines must be within PIOMAXLNSIZE.
The third argument is a boolean option which controls whether lines are automatically chomped or not. It defaults to not.
$nlines = nlslurp($filename, @lines); $nlines = nlslurp($filename, @lines, 1);
A very thin wrapper for slurp that disables file locking.
$rv = piolClose($filename);
This closes all file handles and deletes any existing buffers. Works indiscriminatley and returns the exit value of pclose.
Suffice it to say that when using this module one should only use file names, and use them consistently.
a) the GNU General Public License <https://www.gnu.org/licenses/gpl-1.0.html> as published by the Free Software Foundation <http://www.fsf.org/>; either version 1 <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or b) the Artistic License 2.0 <https://opensource.org/licenses/Artistic-2.0>,
subject to the following additional term: No trademark rights to ``Paranoid'' have been or are conveyed under any of the above licenses. However, ``Paranoid'' may be used fairly to describe this unmodified software, in good faith, but not as a trademark.
(c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com) (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
Copyright © 1997 - 2019, Arthur Corliss, all rights reserved.