...that the AnyCRLF module is automatically putting the crlf-layer on the stack if it is not already available.

Good question.  Like you, I couldn't figure out a way to get at the file handle of the lower layer in either OPEN or PUSHED.  OTOH, as you do have access to the handle in the FILL method, you could use binmode to compose the following hack:

package PerlIO::via::AnyCRLF; sub PUSHED { my ($class) = @_; my $have_crlf; return bless \$have_crlf, $class; } sub FILL { my ($self, $fh) = @_; binmode $fh, ":crlf" unless $$self; $$self = 1; my $len = read $fh, my $buf, 4096; if (defined $buf) { $buf =~ s/\r/\n/g; } return $len > 0 ? $buf : undef; } 1;

Although this works, it doesn't feel right. For one, it violates the principle of least surprise...  as you can see with the debug prints before and after reading from the file handle.

#!/usr/bin/perl use PerlIO::via::AnyCRLF; open my $f, "<:via(AnyCRLF)", "le.txt" or die $!; # debug # print "layers before reading :", join(':',PerlIO::get_layers($f)), " +\n"; # :unix:perlio:via print while <$f>; # print "layers after reading :", join(':',PerlIO::get_layers($f)), " +\n"; # :unix:perlio:crlf:via

The other (similarly suboptimal) way would be to take care of opening a handle yourself:

package PerlIO::via::AnyCRLF; sub PUSHED { my ($class) = @_; my $fh_ref; return bless \$fh_ref, $class; } sub FILL { my ($self) = @_; my $len = read $$self, my $buf, 4096; if (defined $buf) { $buf =~ s/\r/\n/g; } return $len > 0 ? $buf : undef; } sub OPEN { my ($self, $path) = @_; open $$self, "<:crlf", $path or die $!; # debug # print "AnyCRLF layers :", join(':',PerlIO::get_layers($$self)), +"\n"; return 1; } 1;

but in this case, the layer can no longer be properly stacked, as it always becomes the bottommost layer. (Might not be a problem in your particular case, though.)

Unfortunately, the PerlIO::via docs leave a number of questions unanswered (e.g. what is OPEN supposed to return; why does it fail to pass a handle, even if there is in fact a lower layer? etc.), and code samples using OPEN are hard to find.  Well, maybe the "is subject to change" note for the *OPEN methods is meant seriously — and noone has yet worked out in what way they should be changed :)


In reply to Re^5: line ending troubles by almut
in thread line ending troubles by Dirk80

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.