binmode for readling the whole file?

Discussion in 'Perl Misc' started by Peng Yu, Jun 11, 2010.

  1. Peng Yu

    Peng Yu Guest

    To read a whole file into a string. The following webpage uses
    binmode.

    http://www.perlmonks.org/?node_id=1952

    But the following code from perlvar doesn't. I tried both on linux.
    They are identically. But if I want the raw data in the file, I should
    always use binmode to be safe across all OS, right?

    open my $fh, "<", "foo" or die $!;
    local $/; # enable localized slurp mode
    my $content = <$fh>;
    close $fh;
     
    Peng Yu, Jun 11, 2010
    #1
    1. Advertisements

  2. Peng Yu

    Peng Yu Guest

    I had read the document. But I don't what 'appropriate' means. Does it
    mean that I can not use binmode for text files?

    My understand is that in linux, it doesn't matter whether binmode is
    used or not for text files. But windows, binmode should not be used
    for text files, because of the conversion of the CR and LF character.
    Is my understand correct?
     
    Peng Yu, Jun 11, 2010
    #2
    1. Advertisements

  3. Peng Yu

    John Bokma Guest

    /unless/ you want the data as it is on disk, for example to
    calculate a check sum.

    Another reason to use binmode might be if some code has opened the file
    for you and you want to use a different layer, e.g.

    binmode $fh, ':utf8';
     
    John Bokma, Jun 11, 2010
    #3
  4. Peng Yu

    sln Guest

    Semantics! You wouldn't create a check sum on a text file.
    Otherwise it wouldn't be a text file, it would be
    a binary file.

    -sln
     
    sln, Jun 11, 2010
    #4
  5. Peng Yu

    John Bokma Guest

    Is that so? So if I calculate a MD5 digest over a text file to check its
    integrity it magically becomes a binary file? Even if I add the checksum
    to the end of the text file, it still can be a text file.
     
    John Bokma, Jun 11, 2010
    #5
  6. Peng Yu

    Uri Guttman Guest

    JB> Is that so? So if I calculate a MD5 digest over a text file to check its
    JB> integrity it magically becomes a binary file? Even if I add the checksum
    JB> to the end of the text file, it still can be a text file.

    it matters on winblows. if you just open a text file with no binmode and
    pass the handle to a checksum sub, it will not calculate a proper sum
    for the file itself. the cr/lf pairs will become newlines and the sum
    will be different than if done on the raw file.

    uri
     
    Uri Guttman, Jun 11, 2010
    #6
  7. Peng Yu

    sln Guest

    Haha! Its funny but after you validate the integrity with a checksum, it
    reads different when read in text mode depending on the OS.
    Semantics..

    -sln
     
    sln, Jun 12, 2010
    #7
  8. Peng Yu

    John Bokma Guest

    I guess you mean Windows?
     
    John Bokma, Jun 12, 2010
    #8
  9. Peng Yu

    sln Guest

    Oh yeah, for what? Spend a lot of time on it do ya?
    ???? You mean no sense. Its semantics..
    What is a file type and how do you change it?
    Oh yeah, you create checksums on text files all the time.
     
    sln, Jun 12, 2010
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.