Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: crlf layer working on diamond operator, but not filehandle reads

by ikegami (Pope)
on Oct 25, 2011 at 17:37 UTC ( #933662=note: print w/ replies, xml ) Need Help??


in reply to crlf layer working on diamond operator, but not filehandle reads

Should work.

Does work with 5.10.1 and 5.12.1:

$ perl -e'print "foo,bar,baz\r\n";' > myfile.csv $ perl -e' use strict; use warnings; use open IN => ":crlf"; while (<>) { die ("Carriage return in $ARGV") if (/\r/); } open (INPUT, "myfile.csv") or die ("Can'\''t open myfile.csv"); while (<INPUT>) { die("Carriage return in myfile.csv") if (/\r/); } print "ok\n"; ' myfile.csv ok

Are you sure there are no CR other than right before a LF?

if (/\r/) { require Data::Dumper; local $Data::Dumper::Useqq = 1; local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 0; die ("Carriage return in myfile.csv " . Data::Dumper::Dumper( +$_)); }


Comment on Re: crlf layer working on diamond operator, but not filehandle reads
Select or Download Code
Re^2: crlf layer working on diamond operator, but not filehandle reads
by rebugger (Acolyte) on Nov 22, 2011 at 14:43 UTC

    I know I let this thread die, but honestly I never figured out what was going on. Running the exact same code using perl -e and perl myprog.pl produced different results, and I never determined what was causing it.

    However, I figured I should probably say what I ended up doing in case someone ever reads this with a similar problem. Setting :crlf as the current layer by use open IN => ":crlf" only seemed to work for files read by the empty diamond operator, and somehow broke for explicitly opened files, so I simply turned it off after the while(<>) loop with a no open IN => ":crlf", and explicitly added the layer to each file I opened (e.g. open(INFILE, "<:crlf", "myfile.csv")). This seems to always work, but only when the global layer isn't used.

      only seemed to work for files read by the empty diamond operator

      I proved otherwise.

        Your example did not work for me, and I am still unsure why. Output was Carriage return in myfile.csv at -e line 12, <INPUT> line 1. I was getting inconsistent results running it as perl -e '[BLOCK OF CODE]' myfile.csv and as perl code.pl myfile.csv. This was with Perl 5.10.1 running in Cygwin. I'm guessing it has something to do with Cygwin.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://933662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-12-20 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (96 votes), past polls