in reply to HTML and CGI coding

Hi hbradshaw,

print Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"blue", -font-color=> "yellow", -FONT-FAMILY=> "verdana,arial,helvetica", -FONT-SIZE=> "12"});

will not work, but either
print Tr ({align => "CENTER", valign=>"TOP", BGCOLOR=>"blue", "font-color"=> "yellow", "FONT-FAMILY"=> "verdana,arial,helvetica", "FONT-SIZE"=> "12"});

or
print Tr (-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"blue", "-font-color"=> "yellow", "-FONT-FAMILY"=> "verdana,arial,helvetica", "-FONT-SIZE"=> "12");
will work, in the latters case, because a list is being passed in, and the leading - causes some magic to be done on the list. (makes it into a hash in CGI), and the former because you are passing in a hashref, which CGI.pm understands.
In your original caes, CGI thinks you are passing a list in a functional manner, and treating it as such, without trying to match named parameters.

hope this helps

thinker

Update
demerphq is , of course, correct in pointing out that strings before the '=>' containing inline hyphens must be quoted.

Replies are listed 'Best First'.
Re: Re: HTML and CGI coding
by demerphq (Chancellor) on Dec 09, 2002 at 13:43 UTC
    Er. I beg to differ:
    use CGI; use strict; use warnings; print Tr ({align => "CENTER", valign=>"TOP", BGCOLOR=>"blue", font-color=> "yellow", FONT-FAMILY=> "verdana,arial,helvetica", FONT-SIZE=> "12"}); print Tr (-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"blue", -font-color=> "yellow", -FONT-FAMILY=> "verdana,arial,helvetica", -FONT-SIZE=> "12"); __END__ Bareword "font" not allowed while "strict subs" in use at C:\Temp\oops +.pl line 5. Bareword "FONT" not allowed while "strict subs" in use at C:\Temp\oops +.pl line 5. Bareword "FONT" not allowed while "strict subs" in use at C:\Temp\oops +.pl line 5. Execution of C:\Temp\oops.pl aborted due to compilation errors.
    because of the hash key quoting rules that I mentioned earlier...

    --- demerphq
    my friends call me, usually because I'm late....

      Hello: Well, I never realized that trying to change a font color, size, and face was going to cause so much controversy...:) Anyway, I've been modifiying my code and right now it looks as such, I included the whole section of code so you can see what's happening:
      push (@row, Tr ({"-align" => "CENTER", "-valign" => "TOP", "-BGCOLOR" => "blue", "-font-color" => "yellow", "-FONT-FAMILY" => "verdana, +arial,helvetica", "-FONT-SIZE" => "12"}, + th ("I +tem"), th ("Quantity"), th ("Description"), th ("Unit Price"), th ("Price") )); foreach my $item_id (sort (keys (%{$cart_ref}))) { my $item_ref = $cart_ref->{$item_id}; my $total_item_price = $item_ref->{qty} * $item_ref->{price}; $total_price += $total_item_price; # generate a link allowing the item to be deleted from the car +t my $url = sprintf ("%s?choice=delete;item_id=%s", url (), escape ($item_id)); push (@row, Tr ( td (escapeHTML ($item_id)), td (escapeHTML ($item_ref->{qty})), td (escapeHTML ($item_ref->{description})), td ({-align => "right"}, escapeHTML (sprintf ("%.2f", $item_ref->{price +}))), td ({-align => "right"}, escapeHTML (sprintf ("%.2f", $total_item_price +))), + ($show_links ? td (a ({-href => $url}, img ({-src => "../im +ages/delete.jpg", -border => "0"}))) + : td (" ")) )); } push (@row, Tr ( td ({-colspan => "2"}, ""), td ({-colspan => "2"}, "Total"), td ({-align => "right"}, escapeHTML (sprintf ("%.2f", $total_price))) )); return (table ({-border => 0}, @row)); }
      Now, I believe I've incorporated lots of the suggestions but my row with headings have the blue background but it still doesn't want to take the new font color, size, or face. At least I'm not getting any error messages. Any other thoughts are greatly appreciated.

        As I stated before, I think you'd be much better off learning and using CSS for your markup than trying to do it with CGI. You'll get much more consistent results and changes will be a lot easier to make since you'll only have to change one thing in a style sheet rather than who knows how many things in your perl program. Alternately, look into producing your HTML output with HTML::Template rather than CGI. That method would also separate your presentation from your program logic.

        Hi hbradshaw,

        as I pointed out in my earlier post, you should choose either
        Tr( -attr => "value", "-foo-bar" => "baz" )

        ie, attributes have leading hyphens, are quoted if contain inline hyphens, and are not surrounded by {} brackets,
        or
        Tr({ attr => "value", "foo-bar" => "baz" })

        ie, attributes have no leading hyphen, but are contained within {} brackets, making it an anonymous hash. Strings with inline hyphens should still be quoted

        The easiest way to fix your code above is to remove the {} brackets from within your Tr().

        hope this helps

        thinker
        Hi hbradshaw, im afraid my involvement in this thread is restircted to the quoting of hash keys. There are others far more able to advise you on the correct uage of CGI than I am.

        Good luck however, and a little thought, you may find perltidy from sourceforge (dont have a link handy sorry) to be a useful addition to your tool kit.

        Regards,

        --- demerphq
        my friends call me, usually because I'm late....