Re: [MacPerl-WebCGI] well split!! (Solved)
[email protected] (Bruce Van Allen) Thu, 8 Mar 2001 09:25:39 -0800
| Newsgroups | perl.macperl.webcgi |
|---|---|
| Message-ID | <p05010400b6cd643eedf1@[205.179.215.44]> |
At 10:16 AM -0500 3/8/01, [email protected] wrote: > >Bingo. > >I just tried using your ( local $/ = "\r"; ) statement and >everything works without using my (for (split(/\r/))) statement. Yay. >One last question on this subject (I promise). While it is easier >for me, can I proceed safely in my using <tab> and <ret> (i.e., 013 >DEC) for my data files if I subscribe to your ( local $/ = "\r"; ) >statement? Or, should I change the way I'm doing things to be more >"acceptable" in the perl cgi world? In other words, should I change >my <ret> to be both a carriage return and linefeed (i.e., 013 and >015 DEC)? My preference would be to do as little fiddling with EOL/EOR as possible. I'm not clear about the evolution to why you are explicitly setting the EOR in your data file, but if that's worth preserving, then setting $/ before reading the file with the <> input operator is fine. What makes it safe is the localizing. $/ is by default Perl's \n, which within a Perl program means "End of Line/Record in the current OS", as others have pointed out. So, if you change it to \r match your data, you need to make sure to change it back, just in case your script needs to read some other file later that happens to be in the format native to the OS. Notice the outermost '{' and '}' in the example I gave before: { local $/ = "\r"; while (<DATA>) { # ... } } Using local $/ = "\r"; within a block ensures that $/ goes back to whatever value it had before you changed it to \r. Then you don't have to worry about $/ anymore, especially when you go to modify the script next year... I would also put a note with the line altering $/, reminding me that I'm doing that to match the input data { # match incoming data local $/ = "\r"; while (<DATA>) { # ... As for setting the EOR in your data: In the spirit of minimalism, I rarely write out files using an EOL/EOR that isn't native to the OS. Perl makes it very easy to do The Expected Thing using \n. My software is deployed on flavors of UNIX & LINUX, Mac, and NT, and I perhaps obsessively do not write different versions for each OS. Setup files are a way to handle any customization, and not incidentally, are where I handle any non-standard bits like special record separators. The safest way for you to handle a process in which you want to manipulate EOL/EOR is to globalize the separator so your program utilizes the same thing when it writes out data that it uses to parse the data when it reads it in. In a file called something like 'setup.txt' put a line: $rec_sep = \012; # set custom EOR In both the program that writes out your data, and, if different, the one that reads it and parses it, use these lines: use vars qw/$rec_sep/; require 'setup.txt'; Now whenever you write data, terminate records with $rec_sep. And when you read that data in, { # match incoming data local $/ = $rec_sep; # use custom EOR while (<DATA>) { # ... This way, you have control over your EOR throughout your process. OTOH, remember that if you let Perl use its defaults, the EOR manipulations usually "drop out of both sides of the equation." HTH 1; -- - Bruce __bruce_van_allen__santa_cruz_ca__