Binary files in PERL?

Discussion in 'Perl Misc' started by Benjamin Khoo, Nov 7, 2004.

  1. hi all

    i would like to find out how to write a binary file in PERL

    most of the readme and tutorial online only deal with writing ASCII files.
    I would like to create a file with the inputs of the df3 file format.

    can any one help?

    --
    bENJAMIN kHOO
    qIU wENDA
    http://www.copperblue.per.sg
    Benjamin Khoo, Nov 7, 2004
    #1
    1. Advertising

  2. Benjamin Khoo wrote:
    > i would like to find out how to write a binary file in PERL
    >
    > most of the readme and tutorial online only deal with writing ASCII
    > files.


    Are you really on an OS that differentiates between binary and text files?
    Then you may want to check
    perldoc -q binary: "How do I handle binary data correctly?"
    perldoc -f binmode

    jue
    Jürgen Exner, Nov 7, 2004
    #2
    1. Advertising

  3. Benjamin Khoo

    Ben Morrow Guest

    Quoth "Jürgen Exner" <>:
    > Benjamin Khoo wrote:
    > > i would like to find out how to write a binary file in PERL
    > >
    > > most of the readme and tutorial online only deal with writing ASCII
    > > files.

    >
    > Are you really on an OS that differentiates between binary and text files?


    As of 5.8 Perl makes that difference regardless of OS. However, on some
    OSen it defaults to binary and on some to a sort-of mixed text and
    binary mode that is almost certainly completely useless... :)

    Ben

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
    Ben Morrow, Nov 7, 2004
    #3
  4. On Sun, 07 Nov 2004 05:58:41 +0000, Jürgen Exner wrote:

    > Benjamin Khoo wrote:
    >> i would like to find out how to write a binary file in PERL
    >>
    >> most of the readme and tutorial online only deal with writing ASCII
    >> files.

    >
    > Are you really on an OS that differentiates between binary and text
    > files? Then you may want to check
    > perldoc -q binary: "How do I handle binary data correctly?" perldoc
    > -f binmode
    >
    > jue


    Hi jue

    thanks much for your reply..
    i took a look at the documentation,
    does it mean that all i have to do is

    open(FILE,">file.bin");
    binmode (FILE);

    ?

    and then use a print FILE "text\n";
    to write into my file?

    would reading it be jsut the reverse?

    your advice is highly appreciated

    --
    bENJAMIN kHOO
    qIU wENDA
    http://www.copperblue.per.sg
    Benjamin Khoo, Nov 7, 2004
    #4
  5. On Sun, 07 Nov 2004 16:08:19 +0000, Ben Morrow wrote:

    >
    > Quoth "Jürgen Exner" <>:
    >> Benjamin Khoo wrote:
    >> > i would like to find out how to write a binary file in PERL
    >> >
    >> > most of the readme and tutorial online only deal with writing ASCII
    >> > files.

    >>
    >> Are you really on an OS that differentiates between binary and text files?

    >
    > As of 5.8 Perl makes that difference regardless of OS. However, on some
    > OSen it defaults to binary and on some to a sort-of mixed text and
    > binary mode that is almost certainly completely useless... :)
    >
    > Ben


    hi Ben..

    i work on linux.. does that make a difference if it is binary or ascii?
    does that mean that using "binmode" might not give me a binary file as
    expected?

    thanks lots

    --
    bENJAMIN kHOO
    qIU wENDA
    http://www.copperblue.per.sg
    Benjamin Khoo, Nov 8, 2004
    #5
  6. On Mon, 08 Nov 2004 02:15:46 +0800, Benjamin Khoo
    <> wrote:

    >On Sun, 07 Nov 2004 05:58:41 +0000, Jürgen Exner wrote:
    >
    >> Benjamin Khoo wrote:
    >>> i would like to find out how to write a binary file in PERL
    >>>
    >>> most of the readme and tutorial online only deal with writing ASCII
    >>> files.

    >>
    >> Are you really on an OS that differentiates between binary and text
    >> files? Then you may want to check
    >> perldoc -q binary: "How do I handle binary data correctly?" perldoc
    >> -f binmode
    >>
    >> jue

    >
    >Hi jue
    >
    >thanks much for your reply..
    >i took a look at the documentation,
    >does it mean that all i have to do is
    >
    >open(FILE,">file.bin");
    >binmode (FILE);
    >
    >?
    >
    >and then use a print FILE "text\n";
    >to write into my file?
    >
    >would reading it be jsut the reverse?
    >
    >your advice is highly appreciated


    Read about "pack" and "unpack" in the docs. You have to pack the data
    before you write it to a binary file, and unpack it after you read it.
    These functions convert back and forth between textual and binary
    data.
    Steven Sommer, Nov 8, 2004
    #6
  7. Benjamin Khoo

    Ben Morrow Guest

    Quoth Benjamin Khoo <>:
    > On Sun, 07 Nov 2004 16:08:19 +0000, Ben Morrow wrote:
    > > Quoth "Jürgen Exner" <>:
    > >> Benjamin Khoo wrote:
    > >> > i would like to find out how to write a binary file in PERL
    > >> >
    > >> > most of the readme and tutorial online only deal with writing ASCII
    > >> > files.
    > >>
    > >> Are you really on an OS that differentiates between binary and text files?

    > >
    > > As of 5.8 Perl makes that difference regardless of OS. However, on some
    > > OSen it defaults to binary and on some to a sort-of mixed text and
    > > binary mode that is almost certainly completely useless... :)
    > >
    > > Ben

    >
    > i work on linux.. does that make a difference if it is binary or ascii?
    > does that mean that using "binmode" might not give me a binary file as
    > expected?


    binmode will always give you a binary file, and should *always* be used
    for one. Not doing so will lead to perl attempting to interpret the
    contents of the file as (probably) ISO8859-1, and treat these as
    characters, which may have strange effects if you later start using
    Unicode.

    Ben

    --
    For far more marvellous is the truth than any artists of the past imagined!
    Why do the poets of the present not speak of it? What men are poets who can
    speak of Jupiter if he were like a man, but if he is an immense spinning
    sphere of methane and ammonia must be silent? [Feynmann]
    Ben Morrow, Nov 8, 2004
    #7
  8. On Mon, 8 Nov 2004, Ben Morrow wrote:

    > binmode will always give you a binary file, and should *always* be
    > used for one.


    Good advice.

    It might be worth stressing that the documentation for some older
    versions of Perl contains some remarks from Unix bigots which seem to
    imply that only the users of brain-dead operating systems should be
    using binmode(). Ignore them, and get some more-recent documentation.

    > Not doing so will lead to perl attempting to interpret the
    > contents of the file as (probably) ISO8859-1, and treat these as
    > characters, which may have strange effects if you later start using
    > Unicode.


    Indeed. And of course it also makes software less portable to other
    platforms - even in those older Perl versions. Which might be what
    the unix bigots were hoping - but that's hardly to be commended, in a
    language like Perl which aims to be rather platform-neutral.
    Alan J. Flavell, Nov 9, 2004
    #8
  9. "Alan J. Flavell" <> writes:
    [about using and not using 'binmode']
    > Indeed. And of course it also makes software less portable to other
    > platforms - even in those older Perl versions. Which might be what
    > the unix bigots were hoping - but that's hardly to be commended, in a
    > language like Perl which aims to be rather platform-neutral.


    Were you feeling a little conspirational when you wrote the above?
    Arndt Jonasson, Nov 11, 2004
    #9
  10. On Thu, 11 Nov 2004, Arndt Jonasson wrote:

    > "Alan J. Flavell" <> writes:
    > [about using and not using 'binmode']
    > > Indeed. And of course it also makes software less portable to
    > > other platforms - even in those older Perl versions. Which might
    > > be what the unix bigots were hoping - but that's hardly to be
    > > commended, in a language like Perl which aims to be rather
    > > platform-neutral.

    >
    > Were you feeling a little conspirational when you wrote the above?


    My own feelings are surely of little importance here. More to the
    point is what Perl is aiming at.

    And I'm glad to see that subsequent Perl developments have favoured
    portability.

    As it happens, I don't write programs for the Windows environment, and
    I never even used OS/2 in earnest. But as someone who met their first
    computer in 1958, and have used multiple architectures and OSes
    (VAX/VMS might be worthy of note), I do think I have a certain
    weakness for portability, and was not well pleased by those Unix
    bigots who (by their actions) apparently didn't have the confidence
    that unix-like OSes would win on their own merits, and implied that
    they needed to be promoted by bigotry instead.

    all the best
    Alan J. Flavell, Nov 13, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,765
    darshana
    Oct 26, 2008
  2. Marc Schellens
    Replies:
    8
    Views:
    2,983
    John Harrison
    Jul 15, 2003
  3. dermoon
    Replies:
    0
    Views:
    466
    dermoon
    Oct 8, 2003
  4. utab
    Replies:
    3
    Views:
    829
  5. Jim
    Replies:
    6
    Views:
    722
Loading...

Share This Page