MacPerl vs. Unix (OS X) Perl

N

Nathan Olson

Any reason why the following behaves differently from MacPerl to Unix Perl
(in OS 10.2.8)? (Assume all I want to process is the first file; hence the
@ARGV[0].)



open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
@contents = <THISFILE>;
close (THISFILE);


In MacPerl, an ordinary text file is parsed into the @contents array with
each paragraph being a element. But in OS X/Unix Perl, the entire contents
of the text file is placed in @contents[0]; in other words, it comes in as a
long string, not as separate elements. Why the difference? Does it have to
do with line-endings? And how can I change this?

Any help will be appreciated.

Nate Olson
 
A

A. Sinan Unur

Any reason why the following behaves differently from MacPerl to Unix
Perl (in OS 10.2.8)? (Assume all I want to process is the first file;
hence the @ARGV[0].)

open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
@contents = <THISFILE>;
close (THISFILE);

It is too late for me to look up what line end character Mac uses right now
but a simple Google search should yield the answer.

OTOH, it looks like you might want to consult the following FAQ:

perldoc -q @array
 
J

Jim Keenan

Nathan Olson said:
Any reason why the following behaves differently from MacPerl to Unix Perl
(in OS 10.2.8)? (Assume all I want to process is the first file; hence the
@ARGV[0].)



open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
@contents = <THISFILE>;
close (THISFILE);


In MacPerl, an ordinary text file is parsed into the @contents array with
each paragraph being a element.

I don't have MacPerl, so I can't test this statement. But in general
reading a file into an array would result in each *line* -- or, to be
more precise, each *record* -- being a separate element of the array.
The ordinary English meaning of 'paragraph' is a series of lines
separated from another such series by a blank or whitespace-only line.
(See perldoc perlvar on $/.)

But in OS X/Unix Perl, the entire contents
of the text file is placed in @contents[0];

Did you mean to say: $contents[0] ??
 
I

Iain Chalmers

A. Sinan Unur said:
Any reason why the following behaves differently from MacPerl to Unix
Perl (in OS 10.2.8)? (Assume all I want to process is the first file;
hence the @ARGV[0].)

open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
@contents = <THISFILE>;
close (THISFILE);

It is too late for me to look up what line end character Mac uses right now
but a simple Google search should yield the answer.

OTOH, it looks like you might want to consult the following FAQ:

perldoc -q @array

I don't see anything in the original post that suggests a
misunderstanding of arrays... He does indeed have a line ending problem.
He can either change perls understanding of line endings using $\ (see
the input record separator in perldoc perlvar) or change the MacOS9 text
file to a unix text file (and since he's clearly using a Mac, I'll
mentions that BBEdit lets you do this easily).

perldoc perlport has some useful information on this issue:

In most operating systems, lines in files are terminated
by newlines. Just what is used as a newline may vary from
OS to OS. Unix traditionally uses `\012', one type of
DOSish I/O uses `\015\012', and Mac OS uses `\015'.

I notice though, that thats a little out of date these days, perhaps it
should read something like:

In most operating systems, lines in files are terminated
by newlines. Just what is used as a newline may vary from
OS to OS. Unix traditionally uses `\012', one type of
DOSish I/O uses `\015\012', and Mac OS 9 and earlier uses
`\015'. While perl on Mac OS X uses `\012', the OS's
idea of 'text files' varies between `\015' and `\012'.

(as an aside, where's the right place to suggest documentation changes?
here? (as in clpm, not csm) p5p?)

big
 
A

A. Sinan Unur

A. Sinan Unur said:
Any reason why the following behaves differently from MacPerl to
Unix Perl (in OS 10.2.8)? (Assume all I want to process is the
first file; hence the @ARGV[0].)

open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
@contents = <THISFILE>;
close (THISFILE);

It is too late for me to look up what line end character Mac uses
right now but a simple Google search should yield the answer.

OTOH, it looks like you might want to consult the following FAQ:

perldoc -q @array

I don't see anything in the original post that suggests a
misunderstanding of arrays...

@ARGV[0]

perldoc -q @array
 
I

Iain Chalmers

A. Sinan Unur said:
A. Sinan Unur said:
Any reason why the following behaves differently from MacPerl to
Unix Perl (in OS 10.2.8)? (Assume all I want to process is the
first file; hence the @ARGV[0].)

open (THISFILE, @ARGV[0]) || die ("Error opening the file ...");
@contents = <THISFILE>;
close (THISFILE);

It is too late for me to look up what line end character Mac uses
right now but a simple Google search should yield the answer.

OTOH, it looks like you might want to consult the following FAQ:

perldoc -q @array

I don't see anything in the original post that suggests a
misunderstanding of arrays...

@ARGV[0]

perldoc -q @array

Ahhh, OK, I missed that one... Good catch :)

big
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top