Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Special characters ignored

by tigervamp (Friar)
on Oct 03, 2001 at 17:06 UTC ( [id://116432]=perlquestion: print w/replies, xml ) Need Help??

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

I have a file that contains:
ab\r\n\032
That is, the five characters a, b, carraige return, new line, EOF marker.
When I read in the file with conventional means, Perl seems to ignore/lose 2 characters. One example:
undef $/; open(FILE,"test"); $slurp=<FILE>; print length $slurp;
This yields 3. It seems to ignore/lose the CR and EOF character. Why is this happening and how do I make it recognize all the characters so I can work with them easily.

Thanks
tigervamp

Replies are listed 'Best First'.
Re: Special characters ignored
by pjf (Curate) on Oct 03, 2001 at 17:27 UTC
    I just did a test with perl 5.6.1 and 5.005_03 (running on Linux), both of which successfully returned length five with the code and example file you listed above.

    Perhaps you're dealing with an operating system which cares about text files vs binary files, and is stripping the last three characters because it thinks you're in text mode. Try a binmode(FILE) immediately after your open, this should let you read all data (including line-breaks and end-of-file characters) without any interpretation going on.

    Cheers,
    Paul

Re: Special characters ignored
by stefp (Vicar) on Oct 03, 2001 at 17:32 UTC
    Apparently you are using windoze. Have you tried to fiddle with binmode? You probably should.

    -- stefp

      I was using a broken operating system (windows), sorry I failed to mention that. Your binmode worked (thanks), as well as trying the same thing in Linux (without binmode).
      Once again thanks for everyone's quick responses.

      tigervamp

        Just an FYI on Linux and other OS that do not have binary and text mode for files binmode will be optimised out at compile time, so there is no performance or compatability loss from including it.
Re: Special characters ignored
by jepri (Parson) on Oct 03, 2001 at 17:13 UTC
    Perl isn't ignoring them, it's removing them. The <FILE> command reads one line from the file, and strips the carriage return, newline characters.

    To stop it, undefine $/ with local $/;

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      In the original post, tigervamp undefed $/ so I think (IMHO) that it is happening because it doesn't put the EOF marker inside the variable, and that \r\n are taken as one ENTER character, depending on the platform.

      update: removed semicolon after $/ - it won't die here ;~)


      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/
      I did undefine $/, in the first line of my example. I also tried it your way, no luck.

      tigervamp

        Whaddya know, I am an idiot after all.

        To redeem myself slightly I will explain that local $/; is better because at the end of the code block (pretty much anything in curly braces) $/ will return to it's previous value, thus preventing you from mysteriously jinxing other subroutines.

        It doesn't matter in one liners but if you read from two files $/ applies to both - so changing it changes both file handles.

        ____________________
        Jeremy
        I didn't believe in evil until I dated it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://116432]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-06-14 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.