Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Hex value 1A terminates <>

by tekkie (Beadle)
on Jan 13, 2003 at 14:09 UTC ( #226459=perlquestion: print w/replies, xml ) Need Help??

tekkie has asked for the wisdom of the Perl Monks concerning the following question:

Consider the code:
$count = 0; open(FILE, 'test.txt'); while(<FILE>) { $count++; } close FILE; print "$count\n";
Where 'test.txt' resembles:
Arbitrary data Arbitrary data Arbitrary data Arbitrary data <SUB> Arbitrary data Arbitrary data Arbitrary data Arbitrary data
And where <SUB> is the SUBstitute character (Dec 26 Hex 1A Oct 032)

When running on WinNT 4.0 with ActivePerl build 631 the above code prints 4 when the SUB is there, and 9 if that line is left blank.

Has anyone encountered this problem before?
Is there a way to get the <> operator to read past the SUB char?

Edit by Petruchio Thu Jan 16 18:05:09 UTC 2003 - The ^Z character was causing trouble for certain browsers. It was replaced in two places with the string <SUB>, as chosen by the author.

Replies are listed 'Best First'.
Re: Hex value 1A terminates <>
by jmcnamara (Monsignor) on Jan 13, 2003 at 14:19 UTC

    You probably need to binmode the filehandle:
    ... open FILE, 'test.txt' or die "Error message here: $!\n"; binmode FILE; ...

    ASCII 26 is CTRL-Z on Windows and indicates the end of file.


Re: Hex value 1A terminates <>
by derby (Abbot) on Jan 13, 2003 at 14:24 UTC
    Just a guess here but SUB -> HEX 1A -> DEC 26 -> Ctrl-Z -> EOF marker for DOS files? What happens when you apply binmode?


      binmode fixed the problem... thanks to all for helping me realizes what should've been obvious.

      I hate mondays...
Re: Hex value 1A terminates <>
by boo_radley (Parson) on Jan 13, 2003 at 14:18 UTC
    That's an EOF character. Don't they teach control codes in school anymore? Oh, for the days when every computing book had a basic ASCII guide in the back.
    anyway, I believe that you'll have to treat your source file as a binary, and use, e.g., read and seek on them.
    No coffee, too early, mumble mumble, binmode, mumble mumble.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://226459]
Approved by gjb
Front-paged by tye
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-06-02 21:52 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (2 votes). Check out past polls.