Get the is chomped feel of Perl 6 in your Perl 5 code. It works for any readable filehandle, including ARGV.
package IO::Handle; sub is_chomped { tie *{ $_[0] }, 'Perl6::FH', @_ } sub is_not_chomped { untie *{ $_[0] } } package Perl6::FH; use Tie::Handle; sub TIEHANDLE { my ($class, $real, $sep) = @_; ### updated (@_ == 3) -> (@_ > 2) return bless [ undef, 1, @_ > 2 ? $sep : () ], "${class}::ARGV" if $real == \*ARGV; defined fileno($real) or die "$real has no file descriptor!"; open my($fh), "<&" . fileno($real) or die "can't dup to $real: $!"; ### updated (@_ == 3) -> (@_ > 2) bless [ $fh, @_ == 3 ? $sep : () ], $class; } sub READLINE { my $self = shift; my $fh = $self->[0]; return if eof $fh; local $/ = $self->[1] if @$self == 2; my @lines = map { chomp; $_ } wantarray ? <$fh> : scalar <$fh>; return wantarray ? @lines : $lines[0]; } DESTROY { } AUTOLOAD { my $self = shift; my $fh = $self->[0]; (my $meth = $AUTOLOAD) =~ s/.*::/Tie::StdHandle::/; $fh->$meth(@_); } package Perl6::FH::ARGV; @ISA = qw( Perl6::FH ); sub READLINE { my $self = shift; my $fh = $self->[0]; if (not defined $fh or eof $fh) { $self->[1] = 0, push @ARGV, "-" if $self->[1] and not @ARGV; $self->[1] = 1, undef($self->[0]), return unless @ARGV; my $file = shift @ARGV; open $fh, $file or die "Can't open $file: $!"; $self->[0] = $fh; } local $/ = $self->[1] if @$self == 3; my @lines = map { chomp; $_ } wantarray ? <$fh> : scalar <$fh>; return wantarray ? @lines : $lines[0]; } 1;
This module, Perl6::FH, allows you to write code like:
use Perl6::FH; is_chomped STDIN; while (<STDIN>) { # $_ has already been chomp()ed # with whatever $/ is right now }
You can also specify the separator for THAT specific filehandle:
is_chomped STDIN ""; # paragraph mode is_chomped STDIN "%%\n"; # fortune mode
And you can turn it off, and return the filehandle to its normal status:
is_not_chomped STDIN;
The module has been updated to work properly for ARGV (which required some munging):
is_chomped ARGV; while (<>) { # $_ is chomped }

In reply to (updated) Perl6::FH by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.