NAME
Acme::InputRecordSeparatorIsRegexp - Actually, awk doesn't have to be better at something.
VERSION
Version 0.02
SYNOPSIS
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>;
DESCRIPTION
In the section about the "input record separator", perlvar
famously notes
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 filehandle to this package with the appropriate regular expression:
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
.
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.
tie STATEMENT
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 filehandle with the features of this package.
METHODS
open
my $tied_handle = Acme::InputRecordSeparatorIsRegexp->open(
$record_separator_regexp, $mode, @openargs);
Convenience method to open a file, returning a tied filehandle that will read and return records separated according the given regular expression. Returns undef
and sets $!
on error. %tie_opts
if any, are included in the call to tie *HANDLE, 'Acme::InputRecordSeparatorIsRegexp'
.
input_record_separator
my $rs = (tied *$fh)->input_record_separator();
(tied *$fh)->input_record_separator($new_record_separator);
Get or set the input record separator used for this tied filehandle. The argument, if provided, can be a string or a Regexp
object.
chomp
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".
autochomp
my $ac = (tied *$fh)->autochomp;
(tied *$fh)->autochomp($boolean);
Gets or sets the autochomp attribute of the filehandle. If this attribute is set to a true value, readline operations on this filehandle will return records with the record separators removed.
The default value of this attribute is false.
INTERNALS
In unusual circumstances, you may be interested in some of the internals of the tied filehandle 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:
my $th = Acme::InputRecordSeparatorIsRegexp->open( $regex, '<', $filename,
{ bufsize => 65336 } );
bufsize
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.
ALIAS
The package Acme::IRSRegexp
is an alias for Acme::InputRecordSeparatorIsRegexp
, allowing you to write
use Acme::InputRecordSeparatorIsRegexp;
tie *$fh, 'Acme::IRSRegexp',
AUTHOR
Marty O'Brien, <mob at cpan.org>
BUGS, LIMITATIONS, AND OTHER NOTES
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.
Changing $/
will have no affect on a filehandle 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 filehandle.
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.
SUPPORT
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
CPAN Ratings
http://cpanratings.perl.org/d/Acme-InputRecordSeparatorIsRegexp
Search CPAN
http://search.cpan.org/dist/Acme-InputRecordSeparatorIsRegexp/
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2013 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.