[comp.lang.perl] UNIX Perl vs. DOS Perl

todd@pinhead.pegasus.com (Todd Ogasawara) (05/24/91)

I wrote a little program that takes data files of a particular format
(fixed record, no delimiters) and calculates various totals to compare to a
trailer record in that file. The program works fine when I run it using
Perl 4.0 PL03 under UNIX (ISC 2.2, System V/386 R3). However, I get wildly
different results when I run the same program (and using the same data
file) under the DOS version of Perl (3.0 PL41). The outputs are attached
below.

Perl 4.0 PL03 under ISC 2.2
*** FROM TRAILER RECORD ***
Total records         26
Total charges       1991.60
Total adjustments   -363.23
Total allowances     880.91

*** FROM REFERENCE RECORDS ***
Total records         26	<== Trailer and calculated figure match
Total charges       1991.60	<== Trailer and calculated figure match
Total adjustments   -363.23	<== Trailer and calculated figure match
Total allowances     880.91	<== Trailer and calculated figure match


Perl 3.0 PL41 under MS-DOS 3.3
*** FROM TRAILER RECORD ***
Total records         26
Total charges        199.16
Total adjustments    -36.32
Total allowances      88.09

*** FROM REFERENCE RECORDS ***
Total records         26	<== Trailer and calculated figure match
Total charges        199.13	<== ERROR of -0.03000000000000114
Total adjustments    -36.29	<== ERROR of 0.03000000000000114
Total allowances      88.04	<== ERROR of -0.04999999999999716

I checked the data file I brought down to DOS and confirmed that it is
identical to the one I used on the UNIX system.

All I am doing is 'substr' segments of 160 byte long records and
accumulating totals. It works fine under UNIX but fails miserably in the
DOS version. Any ideas? I used a MS-DOS based program that I hoped to
replace with the Perl program to double check things under DOS. The old
program returns results that agree with the UNIX Perl program.
-- 
Todd Ogasawara ::: Hawaii Medical Service Association
Internet       ::: todd@pinhead.pegasus.com
Telephone      ::: (808) 536-9162 ext. 7

lbr@holos0.uucp (Len Reed) (05/28/91)

My first guess is that you have binary/text mode problems.  MS-DOS
perl, like most versions of MS-DOS "C", presume that any file you
operate on is "text."  When you read the file, any CR/LF is changed
to LF; when you write the file, any LF is changed to CR/LF.  In addition,
if you hit a ^Z on input, it's an end-of-file.

Put the following command in your perl script before you do any I/O:
	binmode(handle);
where handle is the perl handle.  E.g.,
	binmode(STDOUT);
	binmode(STDIN);
	binmode(MYFILE);

Do this for each file that manipulates binary data, after the open.

Unix perl will politely ignore these staments.

Next guess--I don't have a 2nd guess.

-Len
-- 
Len Reed
Holos Software, Inc.
Voice: (404) 496-1358
UUCP: ...!gatech!holos0!lbr