Acme::InputRecordSeparatorIsRegexp - awk doesn't have to be better at something.
Version 0.04
use Acme::InputRecordSeparatorIsRegexp; # open-then-tie open my $fh, '<', 'file-with-Win-Mac-and-Unix-line-endings'; tie *$fh, 'Acme::IRSRegexp', $fh, '\r\n|\n|\r'; while (<$fh>) { # $_ could have "\r\n", "\n", or "\r" line ending now } # tie-then-open tie *{$fh=Symbol::gensym}, 'Acme::IRSRegExp', qr/\r\n|[\r\n]/; open $fh, '<', 'file-with-ambiguous-line-endings'; $line = <$fh>; # import open function and use :irs layer use Acme::InputRecordSeparatorIsRegexp 'open'; open my $fh, '<:irs(\r\n|\r|\n)', 'ambiguous.txt'; $line = <$fh>;
In the section about the "input record separator", perlvar famously notes
perlvar
Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-)
This module provides a mechanism to read records from a file using a regular expression as a record separator.
A common use case for this module is to read a text file that you don't know whether it uses Unix (\n), Windows/DOS (\r\n), or Mac (\r) style line-endings, or even if it might contain all three. To properly parse this file, you could tie its file handle to this package with the appropriate regular expression:
\n
\r\n
\r
my $fh = Symbol::gensym; tie *$fh, 'Acme::InputRecordSeparatorIsRegexp', '\r\n|\r|\n'; open $fh, '<', 'file-with-ambiguous-line-endings'; @lines = <$fh>; # or while (my $line = <$fh>) { ... }
The lines produced by the <$fh> expression, like the builtin readline function and operator, include the record separator at the end of the line, so the lines returned may end in \r\n, \r, or \n.
<$fh>
readline
Other use cases are files that contain multiple types of records where a different sequence of characters is used to denote the end of different types of records.
A typical use of this package might look like
my $fh = Symbol::gensym; tie *$fh, 'Acme::InputRecordSeparatorIsRegexp', $record_sep_regex; open $fh, '<', $filename;
where $record_sep_regexp is a string or a Regexp object (specified with the qr/.../ notation) containing the regular expression you want to use for a file's line endings. Also see the convenience method "open" for an alternate way to obtain a file handle with the features of this package.
$record_sep_regexp
Regexp
qr/.../
Another way of using this package to attach a regular expression to the input record separator of a file handle, available since v0.04, is to import this package's open function and to specify an :irs(...) layer.
open
:irs(...)
use Acme::InputRecordSeparatorIsRegexp 'open'; $result = open FILEHANDLE, "<:irs(REGEXP)", EXPR $result = open FILEHANDLE, "<:irs(REGEXP)", EXPR, LIST $result = open FILEHANDLE, "<:irs(REGEXP)", REFERENCE $result = open my $fh, "<:irs(\r|\n|\r\n)", "ambiguous-line-endings.txt"
The :irs(...) layer may be combined with other layers.
open my $fh, "<:encoding(UTF-16):irs(\R)", "ambiguous.txt"
Returns the current setting, or sets the autochomp attribute of a file handle associated with this package. When the autochomp attribute of the file handle is enabled, any lines read from the file handle through the readline function or <> operator will be returned with the (custom) line endings automatically removed.
autochomp
<>
use Acme::InputRecordSeparatorIsRegexp 'open','autochomp'; open my $fh, "<:irs(\R)", 'ambiguous.txt'; autochomp($fh,1); # enable autochomp my $is_autochomped = autochomp($fh); autochomp(tied(*$fh), 0); # disable
This function can also be called as a method on the tied file handle.
(tied *$fh)->autochomp(1); # enable $fh->autochomp(0); # not OK, must use tied handle
Enabling autochomp with this function on a regular file handle will tie the file handle into this package using the current value of $/ as the handle's record separator. If you are just looking for autochomp functionality and don't care about applying regular expressions to determine line endings, this function provides an (inefficient) way to do that to arbitrary file handles.
$/
The default attribute value is false.
Returns the current setting, or changes the setting, of a file handle's input record separator, including file handles that have not already been tied to this package. This overcomes a limitation in IO::Handle::input_record_separator where input record separators are not supported on a per-filehandle basis.
With no arguments, returns the input record separator associated with the file handle. For regular file handles, this is always the current value of $/.
use Acme::InputRecordSeperatorIsRegexp ':all'; open my $fh_reg, "<", "some_text_file"; open my $fh_pkg, "<:irs(\d)", "some_text_file"; $rs = $fh_reg->input_record_separator; # "\n" $rs = input_record_separator($fh_reg); # "\n" $rs = $fh_pkg->input_record_separator; # '\d' $rs = input_record_separator($fh_pkg); # '\d'
With two or more arguments, sets the input record separator for the file handle as the regular expression indicated by the second argument (any argument after the second is ignored). For regular file handles, a side effect is that the file handle will be tied to this package
print ref(tied *$fh_reg); # "" $fh_reg->input_record_separator(qr/\r\n|\r|\n/); print ref(tied *$fh_reg); # "Acme::InputRecordSeparatorIsRegexp"
If you are just looking for the functionality of setting different input record separators on different file handles but don't care about applying regular expressions to determine line endings, this function provides an (inefficient) way to do that for arbitrary file handles. Note that the argument to set the input record separator is treated as a regular expression, so apply quotemeta to it as necessary.
quotemeta
my $chars_removed = (tied *$fh)->chomp($line_from_fh); my $chars_removed = (tied *$fh)->chomp(@lines_from_fh);
Like the builtin chomp function, but removes the trailing string from lines that correspond to the file handle's custom input record separator regular expression instead of $/. Like the builtin chomp, returns the total number of characters removed from all its arguments. See also "autochomp".
chomp
In unusual circumstances, you may be interested in some of the internals of the tied file handle object. You can set the values of these internals by passing additional arguments to the tie statement or passing a hash reference to this package's "open" function, for example:
tie
my $th = Acme::InputRecordSeparatorIsRegexp->open( $regex, '<', $filename, { bufsize => 65336 } );
The amount of data, in bytes, to read from the input stream at a time. For performance reasons, this should be at least a few kilobytes. For the module to work correctly, it should also be much larger than the length of any sequence of characters that could be construed as a line ending.
The package Acme::IRSRegexp is an alias for Acme::InputRecordSeparatorIsRegexp, allowing you to write
Acme::IRSRegexp
Acme::InputRecordSeparatorIsRegexp
use Acme::InputRecordSeparatorIsRegexp; tie *$fh, 'Acme::IRSRegexp',
Marty O'Brien, <mob at cpan.org>
<mob at cpan.org>
Because this package must often pre-fetch input to determine where a line-ending is, it is generally not appropriate to apply this package to STDIN or other terminal-like input.
STDIN
Changing $/ will have no affect on a file handle that has already been tied to this package.
Calling chomp on a return value from this package will operate with $/, not with the regular expression associated with the tied file handle.
Please report any bugs or feature requests to bug-tie-handle-regexpirs at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Acme-InputRecordSeparatorIsRegexp. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-tie-handle-regexpirs at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Acme::InputRecordSeparatorIsRegexp
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Acme-InputRecordSeparatorIsRegexp
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Acme-InputRecordSeparatorIsRegexp
CPAN Ratings
http://cpanratings.perl.org/d/Acme-InputRecordSeparatorIsRegexp
Search CPAN
http://search.cpan.org/dist/Acme-InputRecordSeparatorIsRegexp/
Copyright 2013-2018 Marty O'Brien.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
To install Acme::InputRecordSeparatorIsRegexp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Acme::InputRecordSeparatorIsRegexp
CPAN shell
perl -MCPAN -e shell install Acme::InputRecordSeparatorIsRegexp
For more information on module installation, please visit the detailed CPAN module installation guide.