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

Re: [MacPerl] where to put subroutines



At 6:12 PM -0500 4/11/1999, Jeffrey Hull wrote:
>Here is the problem script, properly saved from MacPerl as a cgi script
>named "phone.acgi"
>
>	#!perl -w
>
>	require "";
># no problems known here; resides at
># MacPerl Ÿ:lib:subparseform.lib

Actually, this line doesn't do anything.  'require' effectively executes
the code found in the file that it references (e.g. require 'my_lib.pl';),
which has the side effect of allowing you to call subroutines defined in
that file, as long as you aren't dealing with modules (which introduce
namespaces and other issues; it's generally best to use 'use' instead of
'require' for them).  Here, you're referencing a file called "", which
probably doesn't exist (note to more experienced Perlers -- why doesn't -w
catch that or require complain about it?).

>
>	&Parse_Form; 		#copied from E. Castro's book
>
>	&mime;			#here is the little lost subroutine
>
>	&header;

A much better way of doing this is with the CGI.pm module, which is
included with MacPerl.  You would accomplish the same as you're trying to
do here with this (it might be a bit off on the style; I've never used CSS):
	use CGI;
	my $query = new CGI;
	print $query->header;
	print $query->start_html(-title => 'Test Page',
			-style => "font-family: Arial;\nfont-size: 9pt");

You can then reference any of the parameters to your form by using
$query->param(paramname) -- for example, $query->param('name').  Take a
look at <http://stein.cshl.org/WWW/software/CGI/cgi_docs.html> or open the
CGI.pm module in Shuck (also part of the MacPerl distribution) to see full,
useful, and well-written documentation for it.

>	#hoping I could get some response to prove it was functional
>	print "$ENV{'REQUEST_METHOD'}\n";

This remains the same.

>	&footer;

When using CGI, this becomes
	$query->end_html;

>And the admittedly trivial practice subroutines (stored of course as
>separate files "mime" "header" "footer")

And that's the problem here.  There's no real reason to store them in
separate files -- at your current point, you could just put them into your
main file (which then would work perfectly).  If you want them in separate
files, you'll have to have a final line of
	1;
at the end of each file (require'd files must return true), and then at the
top of your main script, you'd have this:
	require 'mime';
	require 'header';
	require 'footer';
although they should really be named with .pl extensions, and you could
also combine them into a single file just to be a bit cleaner.

>	sub mime {
>		print "Content-type: text/html\n\n";
>		}

I don't actually think that this'll work, because in MacPerl, '\n'
corresponds to '\r' on Windows and Unix for a variety of reasons that
aren't really worth going into here.  Anyway, here's what CGI.pm does for
this:
-----
	# This is really "\r\n", but the meaning of \n is different
	# in MacPerl, so we resort to octal here.
	$CRLF = "\015\012";
-----

>If it is helpful, at the suggestion of Rich Morin, I saved
>
>foreach $inc (@INC) {
>  print "$inc\n";
>}
>
>as runtime and ran it.  I got this output:
>
>	Server:MacHTTP 2.2.2:MacHTTP Software & Docs:Nurses:cgi-bin:lib:
>	Server:MacHTTP 2.2.2:MacHTTP Software & Docs:Nurses:cgi-bin:lib:
>	:
>	Dev:Pseudo

Those are the directories that require will find files in.  So if you
decided to skip CGI.pm and put your subroutines in files called mime.pl,
header.pl, and footer.pl (or htmlutils.pl for all of them, which would be
cleaner), you'd need the .pl files to be in one of those directories.

Hope this helps,
Eric

--
Eric Albert                     ejalbert@cs.stanford.edu
http://www.stanford.edu/~ejalbert/

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org