PDA

View Full Version : Perl question


daiyu
07-30-2003, 01:39 PM
Hello, this is probably really old news for everyone here but me. Anyway, I was working on a Perl program at home on my OS9 machine, and I had to write the following subroutine in order to get the I/O working between my Mac and a Unix server:

HTTP_Open(); # open connection to Apache server
print $sock "GET /directory/document.htm $crlf$crlf";
while (defined($line = <$sock>)){push(@menu,$line);}
close($sock);
Unix2Mac();
=======

sub Unix2Mac {
@menu = split("\012",$menu[0]);
return(@menu);
}

I passed a Unix document that my Mac thought was a single long line to the $menu[0] scalar, and then split it into the right lines according to the \012 character. Now I had to do this because the MacPerl interpreter didn't understand that \012 lines were line breaks.

I was working on this program today at school, and deleted the call to this subroutine because I thought that OSX's Perl, since it is run from within Darwin, would understand that \012 is the proper carriage return without my having to do the split. I was surprised to find out that it wasn't the case, and I still had to call the subroutine. Why? Does Darwin still use Mac-style line breaks?

I'm not that familiar with Darwin (even less with the niceties of UNIX), and suspect that I'm not even asking the right question. Any help you could give me would be greatly appreciated. Thanks!

Ben
daiyu@mail.utexas.edu

aubreyapple
07-30-2003, 05:11 PM
Logically, I think you were thinking properly. The following may be helpful, however.

First, I believe that the problem with the \012 is not related to the destination OS but the source os. The information from the original server will come across an HTTP connection as is.

Second, on OS9, the default record separator probably is, as you say, not the same as OSX. OSX can store documents with \n, \r, or \r\n record separators and still be called 'TEXT'. However, you can set the default separator in perl by specifying it in the variable $/. I do not do this often, so I am not sure if the command $/=\012 or $/="\012" or some other combination would be the way to specify this.

Gimpy00Wang
07-30-2003, 08:23 PM
Perl tends to get whiny on *nix boxen when you have \r\n's in your script. OS X and other *nix variants use \n's to terminate lines. Windows uses \r\n and I believe, but am not certain that OS9 used \r's only.

You can use hexdump (or Perl for that matter) to see if there's \r's and \n's in a file. Let's say you have a file called foo.txt that contains the following:

lkjlkjlkj
lkjlkjlkj
lkjlkjlkj

Do a "hexdump -c foo.txt" and you should see something like this:
0000000 l k j l k j \n l k j l k j l k j
0000010 \n l k j l k j l k j \n
000001b

I found a short Perl script called dos2unix.pl once over on ONLamp or somewhere that might be of use. It was originally intended to convert \r\n's to \n's, but is easily modified to just replace \r's with \n's if that's what is needed.

- G!mpy

daiyu
07-30-2003, 08:51 PM
Hey guys:

Thanks for all of yall's advice. I've tried reassigning $/ but Perl screamed at me; I'll try Gimpy's advice tomorrow.