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

I'm trying to do some formatting with text in my script. With the code I've written, I keep getting a "bareword" problem. Can someone help me out? Here's the code:
Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"blue", -font-colo +r=> "yellow", FONT-FAMILY=> "verdana,arial,helvetica", FONT-SIZE=> "1 +2"},
The font color, family and size is the issue.

Replies are listed 'Best First'.
Re: HTML and CGI coding
by fruiture (Curate) on Dec 09, 2002 at 12:06 UTC

    the '-' character must not occur inside bare-strings (strings that can stand without quotes, in this case because of =>). In doubt, use quotes!

    UPDATE: in=>inside; of course the leading minus is valid (and even helps perl to recognize unquoted strings), i meant "inside" the string.

    --
    http://fruiture.de
      Sorry, but this is wrong. A leading minus in hash keys is so common that it has been special cased to not be interpreted as a mathematical operation when so used (Im not sure of the exact details of when it is legal). However the same is not true for +, nor is it true for an inline - of which I notice the OP has at least three.
      # inline hyphens will cause problems D:\Temp>perl -MData::Dumper -e "use strict; print Dumper({-leading_min +us=>1,+plus=>1,in-line-minus=>1})" Bareword "in" not allowed while "strict subs" in use at -e line 1. Bareword "line" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors. # what happened to the plus? D:\Temp>perl -MData::Dumper -e "use strict; print Dumper({-leading_min +us=>1,+plus=>1,'in-line-minus'=>1})" $VAR1 = { 'in-line-minus' => 1, '-leading_minus' => 1, 'plus' => 1 }; # this works fine D:\Temp>perl -MData::Dumper -e "use strict; print Dumper({-leading_min +us=>1,'+plus'=>1,'in-line-minus'=>1})" $VAR1 = { 'in-line-minus' => 1, '-leading_minus' => 1, '+plus' => 1 };
      Cheers,

      UPDATE:If in doubt use either Text::Quote->quote_key() or qw:

      {qw( -align CENTER -valign TOP -BGCOLOR blue -font-color yellow -FONT-FAMILY verdana,arial,helvetica -FONT-SIZE 12 )}

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

        This was my first assumption, however, command-line testing contradicted this: (Warning, I'm on unix, looks like you're on Win32 - adjust quotes accordingly)

        sh$ perl -MCGI -e '$q = new CGI ; print $q->Tr({-font-family => "not-v +alid"})'
        Gives:
        <tr 0="not-valid" />
        Which appears to contradict my assumption, excusing brainfarts.

        davis
        Is this going out live?
        No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist

        Update: Thanks for the explanation, LTjake
        Update 2: Thanks also to demerphq.
      Hey, I modified my code to look like this.
      ({-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"blue", -"font-color +"=> "yellow", -"FONT-FAMILY"=> "verdana,arial,helvetica", -"FONT-SIZE +"=> "12"},
      I also have "use strict" at the top of my script. I run the script, no errors but none of the font formats appear.

        Ignoring HTML validity for a moment:

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

        is probably more helpful. Hint: Keep quoting (and most other syntax) consistent, and it's easier to debug

        cheers
        davis
        Is this going out live?
        No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist

        Update: Sheesh. Two instances of the word "probably" in one sentence? Yuck.
Re: HTML and CGI coding
by thinker (Parson) on Dec 09, 2002 at 13:38 UTC
    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.
      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.