Acme-InputRecordSeparatorIsRegexp
view release on metacpan or search on metacpan
lib/Acme/InputRecordSeparatorIsRegexp.pm view on Meta::CPAN
where C<$record_sep_regexp> is a string or a C<Regexp> object
(specified with the
L<< C<qr/.../>|"Quote and quote-like operators"/perlop >> notation)
containing the regular expression
you want to use for a file's line endings. Also see the convenience
method L<"open"> for an alternate way to obtain a file handle with
the features of this package.
=head1 FUNCTIONS
=head2 open
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 C<open> function and to
specify an C<:irs(...)> I<pseudo-layer>.
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 C<:irs(...)> layer may be combined with other layers.
open my $fh, "<:encoding(UTF-16):irs(\R)", "ambiguous.txt"
See also: L<"binmode">
=head2 autochomp
Returns the current setting, or sets the C<autochomp> attribute
of a file handle associated with this package. When the
C<autochomp> attribute of the file handle is enabled, any lines
read from the file handle through the C<readline> function
or C<< <> >> operator will be returned with the (custom) line
endings automatically removed.
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 I<tied>
file handle.
(tied *$fh)->autochomp(1); # enable
$fh->autochomp(0); # not OK, must use tied handle
Enabling C<autochomp> with this function on a regular file handle
will tie the file handle into this package using the current
value of C<$/> 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.
=head2 binmode FILEHANDLE, LAYER
Overrides Perl's builtin L<binmode|perlfunc/"binmode"> function.
If the I<pseudo-layer> C<:irs(...)> is specified, then apply the
given regular expression as the dynamic input record separator for
the given filehandle.
Any other layers specified are passed to Perl's builtin C<binmode>
function.
=head2 input_record_separator
Returns the current setting, or changes the setting, of a file handle's
input record separator, I<including file handles that have not
already been tied to this package>. This overcomes a limitation
in L<IO::Handle::input_record_separator|IO::Handle/"METHODS">
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 L<< C<$/>|perlvar/"$INPUT_RECORD_SEPARATOR" >>.
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 C<quotemeta> to it as necessary.
=head1 METHODS
=head2 chomp
my $chars_removed = (tied *$fh)->chomp($line_from_fh);
my $chars_removed = (tied *$fh)->chomp(@lines_from_fh);
Like the builtin L<< C<chomp>|"chomp"/perlvar >> function,
but removes the trailing string from lines that correspond to
( run in 1.965 second using v1.01-cache-2.11-cpan-5a3173703d6 )