Re: Chomp to trim '\r'
On Wed, 8 Oct 2003, Richard Morse wrote: > To the best of my knowledge, binmode isn't a > "Windows-only" function. It's just that the most common > operating systems besides windows don't make distictions > between text and binary files, so it isn't useful on *nix > and its variants. Apparently some older operating systems > did make such a distinction (some made others as well), > and it would be a necessary call on those systems. > > Further, I think you do need binmode to handle non-text files properly. > Even sysread/read will stop reading a text-mode file > when they hit the control-D character (I had this happen > to me) -- you have to put the file in binary mode to > continue reading past the character. That's a good point that binmode() in principle isn't Windows specific, especially when one throws the use of io layers into the mix. For example, == use strict; use warnings; my $scratch = 'scratch.dat'; my $utf8 = "\x{042F} \x{0432}\x{0430}\x{0441} \x{043B}\x{044E}"; open my $wfh, ">", $scratch or die "Cannot open $scratch for writing: $!"; binmode($wfh, ':utf8'); print $wfh $utf8; close $wfh; open my $rfh, "<", $scratch, or die "Cannot open $scratch for reading: $!"; binmode($rfh, ':utf8'); my $text; { local $/; $text = <$rfh>; } close $rfh; binmode(STDOUT, ':utf8'); print $utf8, "\n", $text, "\n"; == needs the binmode calls, even on unix. -- best regards, randy kobes ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Chomp to trim '\r'
Carl Jolley wrote: After all, all binmode is, is a way to turn off perl's default way of handling the "\r" for you. It's your way of saying to perl "Thanks, but no thanks for your offer of assistance, For this file, I can handle this issue without your help". While this is true within the context of this discussion (newline conversions), binmode does other things that can also be very important. For instance, I found out several years ago that it was necessary to use binmode when opening some Word97 files in Perl scripts (under Win98), or some of the files would be truncated in the proprietary MS header. As I recall, the specific problem was a \000 byte early in the header that Perl would treat as an eof (unless binmode was in use), but I understand that binmode also turns off Perl's default handling of embedded Ctrl-Z and other control characters. So I'm not disagreeing with you, but simply pointing out that whenever the input file is in a proprietary format that mixes text with binary segments, binmode is something to think about. At least under Windows, DOS, and related OSs. And then there's the whole disciplines thing that can be done with binmode (or a 3 argument open). But I don't go there; it is my fervent hope that Perl 6.0 will fix up all that stuff before I have to deal with any multibyte unicode data. :-) -- Will Woodhull [EMAIL PROTECTED] ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Chomp to trim '\r'
On Wednesday, October 8, 2003, at 12:52 AM, Carl Jolley wrote: Anyway that's why binmode is a Windows-only function. A common mis-conception among windows perl'ers is that somehow binmode is needed to properly handle a binary/non-text mode file. To the best of my knowledge, binmode isn't a "Windows-only" function. It's just that the most common operating systems besides windows don't make distictions between text and binary files, so it isn't useful on *nix and its variants. Apparently some older operating systems did make such a distinction (some made others as well), and it would be a necessary call on those systems. Further, I think you do need binmode to handle non-text files properly. Even sysread/read will stop reading a text-mode file when they hit the control-D character (I had this happen to me) -- you have to put the file in binary mode to continue reading past the character. Ricky ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Chomp to trim '\r'
On Wed, 8 Oct 2003, Sisyphus wrote: > Lee Goddard wrote: > > > > > Why not use a regular expression? > > > > I can't find an answer to that question - and, judging by the responses > so far received, nor can anyone else !! > > ;-) > You know that in perl "TAMTOWTDI". However usually there are some ways that are more-or-less equivalent and then there are ways that only a veteran of several obfuscated perl contests could appreciate. [EMAIL PROTECTED] All opinions are my own and not necessarily those of my employer ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Chomp to trim '\r'
Lee Goddard wrote: Why not use a regular expression? I can't find an answer to that question - and, judging by the responses so far received, nor can anyone else !! ;-) Cheers, Rob -- Any emails containing attachments will be deleted from my ISP's mail server before I even get to see them. If you wish to email me an attachment, please provide advance warning so that I can make the necessary arrangements. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Chomp to trim '\r'
> > Why not use chop? That removes the last character. > > > > Yes, it certainly does even if the last character is not a > "\r" character. > So to do the work of one good regex substitute operator as > Lee suggested > you would have to determine some how (a regexp match operator?, a > substring to get the last character followed by a compare to see if it > was a "\r", etc.). Those two distinct operations combined > with a separate > compare step should be evaluated as an alternative to using a single > substitute operator which will remove the last character if it is > there and tell you if it did or didn't remove a "\r". Hmmm, which of > those seems like the better alternative? Do I want my script to > use four or five obtuse op codes to perform a minor function point or > do I want to use just one elegant operator do accomplish the exact > same minor function point? Oh. I thought all the lines ended in a "\r". Sorry. R. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs