[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Re: Use the proper syntax, Luke



At 05:59 +1000 on 17/7/97, Paul J. Schinder wrote:
>Strider <Strider@clarityconnect.com> writes:
>}$name = $contents['name'];
>}$addy1 = $contents['addy1'];
>}$addy2 = $contents['addy2'];
[snip]
>
>Are these supposed to be hashes?  If so, write them properly, i.e.
>$contents{'category'}....

Also, I'd advise Strider to use the following when appending data to the
classifieds.data file (the code is not tested, in addition it only reflects
my own opinion and idiom):

	#!perl -w
	use strict;
	use English;

	# Rest of CGI handler goes here... loading the values
	# for name, address fields, product information, etc.
	# Don't forget "my" and "local" as appropriate...

	$OUTPUT_FIELD_SEPARATOR = "\t";
	$OUTPUT_RECORD_SEPARATOR = "\n";
	# Restoring these to what they were three lines ago
	# is an excercise for the reader :-)

	open FILE, ">>classifieds.data";
	print FILE $name, $addy1, $addy2, $city, $state, $zip,
		$zip2, $phone, $email, $category, $subject,
		$info, $price, $time;
	close FILE;

Doing it this way (using $, and $\) lets PERL take care of the formatting.
You just tell it what you want, not how it's supposed to do it. An added
bonus is that here we're *appending* to the file, rather than loading the
whole thing in, modifying it in memory, and writing it out again.

Some other items of my opinion that I just *have* to get out:
 - Try using tabs, instead of newlines, as your field separator
    (this is achieved using the $, or $OUTPUT_FIELD_SEPARATOR
     magic variables - don't forget to "use English;")
 - Try using newlines as record serarators only
    (see above, using $\ or $OUTPUT_RECORD_SEPARATOR)
 - Don't forget to put an exit of some sort on the validity check,
   otherwise the script tries to write bad records to the
   classifieds file
 - Consider having a "date submitted" record in the classifieds file,
   so you know when the 1 to 8 weeks expires.
 - Use the "-w" option (make the first line #!perl -w)
 - put "use strict;" just after the first line
 - Test PERL scripts in stand-alone mode before running them as CGIs,
   so you don't have to try figuring out what went wrong at what point
   in the browser-httpd-PERL chain.
 - I'd be tempted to remove the "-w" and "use strict;" options once
   the script was working, but I'd want proof that this made the
   script run faster or use less memory.

The "use strict" line is like having a nagging mother. Sure, she can be
*so* annoying sometimes, but the worst part is, she's usually right :-)

-Alex
(I just *know* there's a mistake in here somewhere... can you point it out
to me please :-)

finger packrat@ucnet.canberra.edu.au for pgp public key.



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch