Mailing List Archive

read() with offset returns crap
$ echo abcde >foo; perl -e 'open A, foo or die; read (A, $a, 1, 3); print $a, "\n";'
Ka
$ perl -e 'open A, foo or die; seek (A, 3, 0); read (A, $a, 1); print $a, "\n";'
d

That is, read() returns crap if given fourth argument

Happens on:

perl 5.001m
Linux woods 1.2.11 #1 Wed Aug 30 21:41:02 EET DST 1995 i586

but not for example on

HP-UX lk-hp-14 A.09.03 A 9000/705 2015871779 two-user license
or
OSF1 beta.hut.fi V3.2 148 alpha

--
Samuli Karkkainen - hskarkka@snakemail.hut.fi - http://www.hut.fi/~hskarkka/
Re: read() with offset returns crap [ In reply to ]
Excerpts from the mail message of Samuli K{rkk{inen:
)
) $ echo abcde >foo; perl -e 'open A, foo or die; read (A, $a, 1, 3);
)-print $a, "\n";'
) Ka
) $ perl -e 'open A, foo or die; seek (A, 3, 0); read (A, $a, 1);
)-print $a, "\n";'
) d

You seem to misunderstand what the "offset" is refering to. It
is _not_ an offset from the front of the _file_, as your second
example implies that you think it means. From "man perl"~ (with
emphasis added):

read(FILEHANDLE,SCALAR,LENGTH,OFFSET)
[...] An OFFSET may be specified to
/place/ the read data /at/ some other place than the
beginning of the /string/.

) That is, read() returns crap if given fourth argument
)
) Happens on:
[...]
) but not for example on

Actually it probably happens in both places but you just didn't
notice it on some of them. Try:

% echo abcde | perl -e 'read(STDIN,$a,3,3); print "$a\n"'
abc
% echo abcde | perl -e 'read(STDIN,$a,3,3); print "$a\n"' | cat -vet
^@^@^@abc$
%

So three nul charaters of garbage were actually added but they
are hard to see unless you really look for them.
--
Tye McQueen tye@metronet.com || tye@doober.usu.edu
Nothing is obvious unless you are overlooking something
http://www.metronet.com/~tye/ (scripts, links, nothing fancy)
~Sorry, but with Perl4's "man perl" being a single file, I still
use it more often than Perl5's "perlfunc.pod", etc.