Binary files in PERL?

B

Benjamin Khoo

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?
 
J

Jürgen Exner

Benjamin said:
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
 
B

Ben Morrow

Quoth "Jürgen Exner said:
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
 
B

Benjamin Khoo

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
 
B

Benjamin Khoo

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
 
S

Steven Sommer

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.
 
B

Ben Morrow

Quoth Benjamin Khoo said:
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
 
A

Alan J. Flavell

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.
 
A

Arndt Jonasson

Alan J. Flavell said:
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?
 
A

Alan J. Flavell

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top