Re: binmode and <>



Larry wrote:
Dr.Ruud wrote:
Larry schreef:
Larry:

Is there any way to get "while (<>)" to use binmode? I can't figure
it out since binmode is supposed to be called after the file is
already open but before it is read for the first time, so you can't
call it before the loop, nor can you call it inside the loop.

I would also like to use binmode for 1-liners using -n or -p
switches.

P.S. ... in case anyone is wondering why I would want to use <>
together with binmode (they usually don't go together) is that I have
a .csv file generated by Excel. Each record is terminated by "\r\n",
however inside each record there are some "\n" characters,
representing multi-line cells in the spreadsheet. So I want to set
$/ = "\r\n" and turn on binmode.

Try

perl -MO=Deparse -ne1

and you'll find the name of the handle.


I know the handle is ARGV, but knowing the name won't help. The handle
won't be open before the "while" loop so I can't "bindmode" it then.
Inside the "while" loop, it will already be too late to call "binmode",
since the line has already been read.

I think he meant you could binmode ARGV in a BEGIN block (untested) but
John's suggestion was, as usual, The Right Answer :P

-jp

.



Relevant Pages

  • Re: binmode and the diamond operator
    ... > ARGV, but open and then binmode each file yourself), which pretty much ... the following code to use the diamond operator in binmode (on Win32 ... binmode on the filehandle, so that the number of characters reported ... And calling it as the first line of the while loop is too late, ...
    (comp.lang.perl.misc)
  • Re: binmode and the diamond operator
    ... >> I use the diamond operator in a while loop after setting slurp ... > binmode ARGV; ... Should I put it before the "while " loop or inside it? ...
    (comp.lang.perl.misc)
  • Re: file upload - get the file size problem
    ... use CGI; ... At what point is $bytesread zero? ... And why do you binmode SAVE inside the loop? ...
    (comp.lang.perl.misc)
  • Re: binmode and <>
    ... out since binmode is supposed to be called after the file is already ... open but before it is read for the first time, ... before the loop, nor can you call it inside the loop. ... -- Larry Wall ...
    (comp.lang.perl.misc)
  • Re: binmode and <>
    ... Larry schreef: ... already open but before it is read for the first time, ... call it before the loop, nor can you call it inside the loop. ... I would also like to use binmode for 1-liners using -n or -p ...
    (comp.lang.perl.misc)