jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

The sample code below is from Ovid's CGI Course. I keep trying to teach myself how to code with CGI qw/:standard/ but I can't figure it out. My question is what is the logic which determines when one must end the line with a comma. One of the TD closures does not have one and the code works but Lord hep ya if you mess with any of the others.

I also am confounded that I cannot seem to break out from the print command in after I start the table but before creating cells. Other than Ovid's fine course can someone direct me to a tute. I really prefer rolling my own HTML cause I get it, but the other monks keep teasing me, err...
TIA
jg

table( { -bgcolor => "#000000", -border => "0", -cellpadding => "2", -cellspacing => "1", -style => "font: 10pt;" }, Tr( { -style => "background-color:#CCCCCC" }, td( strong( "User Name:" ) ), td( input( { -maxlength => "30", -name => "username", -size => "30", -type => "text"} ) ) # end td ), # end Tr
_____________________________________________________
If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ

Replies are listed 'Best First'.
(tye)Re: ??Using Commas in CGI qw/:standard/ to make HTML??
by tye (Sage) on Nov 27, 2001 at 11:06 UTC

    You could just always end the line with a comma. That often leads to code that is easier to maintain anyway.

    But the comma is required (whether at the end of the line or not) if it is needed to separate two items. So you don't need a comma before a closing ) or }, for example, but you can put one there if it would be convient (especially if future changes to the code might lead you to add additional items after that line).

    If you do include a "trailing" comma, then you should also move the closing bracket to the next line:

    table( { -bgcolor => "#000000", -border => "0", -cellpadding => "2", -cellspacing => "1", -style => "font: 10pt;", }, Tr( { -style => "background-color:#CCCCCC" }, td( strong( "User Name:" ) ), td( input( { -maxlength => "30", -name => "username", -size => "30", -type => "text", } ) ), }, ), # end Tr );
    I also think your indentation could be more regular. (:

    Note how in my code above, you can insert, delete or rearrange items in any of the lists by inserting, deleting, or rearranging lines without having to adjust opening or closing brackets or commas. That is usually a good thing.

            - tye (but my friends call me "Tye")
Re: ??Using Commas in CGI qw/:standard/ to make HTML??
by jarich (Curate) on Nov 27, 2001 at 17:00 UTC
    I also am confounded that I cannot seem to break out from the print command in after I start the table but before creating cells.

    What an unusual thing to want to do! :P

    No seriously, the reason you can't stop printing etc, without printing data etc, is that when you call table() the table is closed where that close parentheses is. You can't leave the parentheses hanging open because that is a syntax error. So you can do this:

    print table();
    but obviously not this:
    print table(;
    So, how do you get a start table, and an end table that allows you to create data between (with a foreach loop for example)? Glad you asked... What you have to do is this:
    use CGI qw/:standard start_table end_table/; # actually you can do: # use CGI qw/:standard *table/; too I think. print start_table; # make some table data print end_table;
    It's the same to get start_Tr, and any similar stuff. pjf's node links to the course notes that we use to teach CGI courses, feel free to use these if it helps.
Re: ??Using Commas in CGI qw/:standard/ to make HTML??
by IlyaM (Parson) on Nov 27, 2001 at 16:05 UTC
    IMHO there is no reason to spend any time to learn to hardcode HTML code in your Perl script. Using templates for separation of HTML from code is much more robust aproach. CPAN has a number of modules for templates. For example HTML::Template. IMHO CGI.pm is only useful for param parsing and handling file uploads.