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

assign cgi oo code to a variable inside a loop and then use it to output to the page/file.

here is the code that I am trying :-
while ($dy < 32) { if ($dow eq "0" || $dow eq "6") { $table_info = $table_info."$q->({align => /'left/', -color => /'yellow/'}, $q->th([$...)),"; } else { $table_info = $table_info."$q->({align => /'left/', $q->th([$...)),"; } $dy++; } print OUTPUT $q->table({-cellpadding => 3}, $table_info, );

however the table rows etc are not created and I end up with text in one block and CGI HASH everywhere.

All I want to do is to highlight the weekend dates...

thanks in advance

2006-07-28 Retitled by GrandFather, as per Monastery guidelines
Original title: 'How do I .......'

Replies are listed 'Best First'.
Re: How do I use CGI.pm methods in a loop
by davorg (Chancellor) on Jul 28, 2006 at 10:14 UTC

    You don't seem to be calling the Tr function. Also, you seem to expect method calls to be expanded in double-quoted strings - which they aren't. Oh, and you're trying (unnecessarily) to quote characters with the wrong kind of slash.

    while ($dy < 32) { if ($dow == 0 || $dow == 6) { $table_info .= $q->Tr({-align => 'left', -color => 'yellow'}, $q->th([$...])); } else { $table_info .= $q->Tr({-align => 'left'}, $q->th([$...])); } $dy++; }

    With code like that I'd assume that you'd get a lot of syntax errors. Did you check the web server error log?

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      To OP -- since all you're trying to do is make certain rows a different color, then you can have the code reflect that as well, making it easier to read and maintain:
      while ($dy < 32) { $table_info .= $q->Tr({ -align => 'left', ($dow == 0 || $dow == 6 ? (-color => 'yello +w') : () ), }, $q->th([$...]), ); $dy++; }
      Then from here, we can go even further and use map (and use "THE DISTRIBUTIVE PROPERTY OF HTML SHORTCUTS" -- see CGI pod):
      print OUTPUT $q->table({-cellpadding => 3}, $q->Tr({-align => 'left'}, [ map { $q->Td( { ($dow == 0 || $dow == 6 ? (-color => 'yellow') : ( +)) }, [ ... ] ) } 0 .. 31 ]), );
A reply falls below the community's threshold of quality. You may see it by logging in.