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

Re: [MacPerl-Modules] Generating HTML Code via MacPerl



At 10:14 AM 12/10/00, sleidy@voicenet.com wrote:
>I have created a '.cgi' File which includes the partial Code below -
>
>use CGI ':standard';

[large snip]

>My understanding of the above -
>
>There are, at least, three [3] Formats to generate an HTML Web Page 
>in [Mac]Perl.
>
>1)   print start_form .... ,
>2)   print DATA "<table width=\"598\" border=\"2\" cellspacing=\"4\" 
>cellpadding=\"0\">"  ......,
>3)   print $form->startform .....
>
>OK ok I did not have any 'form' Code in the second Example - but it 
>is the format I am trying to convey here ...
>
>So - and simply asked -
>Where does one obtain specific Documentation listing all the 
>Functions and their operations for each of the above displayed 
>Formats?

Seems like you're confused by some methods (functions) provided by 
the CGI.pm module. In your code, all of the functions providing HTML 
markup are really methods imported from CGI.pm (start_html(), 
start_form(), a(), b(), br, textfield, etc).

So, you'll need to spend some time learning about CGI.pm. The best 
sources are the author's excellent documentation:

    Book: Official Guide to Programming with CGI.pm
          by Lincoln Stein, Wiley, 1998;

    Online: http://www.wiley.com/compbooks/stein/.

In this discussion, allow yourself to assume that the terms 'method', 
'function', and 'subroutine' mean the same thing unless specifically 
noted.

Your #1 and #3 above are different ways of utilizing CGI.pm's 
HTML-writing powers.

If you start your script with an invocation of CGI.pm like this:

   use CGI ':standard';

what you've just done is make the subroutines of CGI.pm's :standard 
function set available in your script, pretty much as though they 
were Perl functions. This allows you to use constructs like:

   print start_form;
   print h1(...), h2(...), p(...), p(...), h2(...), p(...), p (...);

This is accomplished with Perl's economical approach to modules. 
These subroutines are what's producing most of the HTML output in 
your code examples.

The second approach to accessing CGI.pm's methods (subroutines) is to 
create a set of calls to those methods, and bundle the set up in a 
single variable, from which you may access the methods as needed. 
This is the object-oriented approach. The variable holding references 
to all the subroutines is the object.

Start like this:

   use CGI;             # no need to import function sets
   $query = new CGI;    # $query is now a CGI object
   $query = CGI->new(); # same thing

Access the methods (subroutines) like this:

   print $query->start_form();
   print $query->h1(...),
         $query->h2(...), $query->p(...), $query->p(...),
         $query->h2(...), $query->p(...), $query->p(...);

In one of your code examples, you have:

    use CGI::Form;
    $cgi_form = new CGI::Form;

So you created a CGI::Form object embodied in the variable $cgi_form. 
To use its methods, employ the object like this:

    print $cgi_form->start_form;

Unfortunately, in your example code there are several uses of

    $form->startform;
    $form->radio_group

but you hadn't created an object in the variable $form.

Also, are you sure you want to use a module named CGI::Form? I'm 
familiar with CGI.pm's :form subset of form-related functions, but 
that would be called as follows:

    use CGI ':form';
    use CGI qw/:form/; # same thing

or perhaps with other function sets:

    use CGI qw/:form :standard/;

If there isn't a CGI::Form module available, you'll get an error.

Finally, to be clear, Perl/MacPerl doesn't have any native functions 
that produce HTML output. But some of the modules that are included 
with the standard Perl installation do, and CGI.pm is one of them. 
CGI.pm's main use, of course, is for handling the whole CGI process, 
not just creating HTML markup. Well worth studying if you're doing 
Internet programming.

Besides functions/methods/subroutines, Perl/MacPerl offers plenty of 
ways to produce text output. For producing HTML, I mostly rely on 
templates kept separate from the script. But if I want to produce 
HTML right out of my code, here's a good way to print multi-line HTML 
with inserted data:

# a "here-doc" with variable interpolation
# all Perl variables previously populated

print "Content-type: text/html\n\n";

print <<HTML_PAGE;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
      "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
   <TITLE>$inst_title Admin</TITLE>
</HEAD>
<BODY>

<A NAME='TOP'> </A>

<DIV ALIGN=CENTER>

<H2>$inst_title<BR>Admin</H2>
<HR>
  <A HREF="$home">$inst_title Monitor</A> ||
  <A HREF="http://$inst_url">$inst_title Web Site</A> ||
  <A HREF="../documentation/help.html">Help</A>
<HR>
$inst_page_content
<HR>
  <A HREF='#TOP'>Return to Top</A> ||
  <A HREF="$home">$inst_title Monitor</A> ||
  <A HREF="http://$inst_url">$inst_title Web Site</A> ||
  <A HREF="../documentation/help.html">Help</A>
<HR>

</DIV>

</BODY>
</HTML>

HTML_PAGE

Multi-line output in one print statement, variables interpolated into 
values, indentation and white space preserved, quotes preserved, no 
escaping needed. Very nice for HTML output; here-docs go way deeper, 
but this is a start.

HTH;

1;

- Bruce

__Bruce_Van_Allen___Santa_Cruz_CA__

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