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

I would like to know what syntax I should use to introduce a colspan in the middle of a row (as in example '2' in the code below) when using CGI.pm to generate HTML. Obviously, my guess is wrong (and I didn't find any alternate syntax in the book):
print $q->table({-border=>'1', -cellpadding=>'2', -cellspacing=>'2'}, Tr({-align=>'LEFT', -valign=>'CENTER'}, [ $q->td(['one', '1', 'I']), $q->td([ 'two' ], {-colspan=>'2'}, [ '2' ]), $q->td({-colspan=>'2'}, ['three', 'III']), $q->td({-colspan=>'3'}, ['four']), ]));
Here's the output of the above code (minus some of the formatting):
<TABLE> <TR> <TD>one</TD> <TD>1</TD> <TD>I</TD> </TR> <TR> <TD>two</TD> </TR> <TR> <TD COLSPAN="2">three</TD> <TD COLSPAN="2">III</TD> </TR> <TR> <TD COLSPAN="3">four</TD> </TR> </TABLE>
And this is what I would like for number two:
<TR> <TD>two</TD> <TD COLSPAN=2>2</TD> </TR>

Replies are listed 'Best First'.
Re: Mid-row colspans with CGI.pm
by tye (Sage) on Oct 02, 2000 at 22:06 UTC

    Well, I almost had it right the first time:

    #!/usr/bin/perl -w use strict; use CGI::Pretty ":standard"; my $q= CGI->new(); print $q->table( { -border=>1, -cellpadding=>2, -cellspacing=>2 }, Tr( { -align=>'LEFT', -valign=>'CENTER' }, [ $q->td( [ 'one', 1, 'I' ] ), $q->td(['one', '1', 'I']), $q->td(['two']) . $q->td({-colspan=>'2'}, ['2']), $q->td({-colspan=>'2'}, ['three', 'III']), $q->td({-colspank=>'3'}, ['four']), ] ) );

    Thanks to jcwren for including fully functional code so I could test what I wrote.

            - tye (but my friends call me "Tye")
Re: Mid-row colspans with CGI.pm
by tye (Sage) on Oct 02, 2000 at 21:28 UTC

    It looks like a simple change of

    $q->td([ 'two' ], {-colspan=>'2'}, [ '2' ]),
    to
    $q->td([ 'two' ]), $q->td({-colspan=>'2'}, [ '2' ]),
    is all that is required.

    Update: Thanks, jcwren. It felt like I must have been missing something... and I was!

            - tye (but my friends call me "Tye")
      Except that it produces as extra table row.

      And in reply to Ovid's post, that's about the conclusion I came to. Also, if you have more than one array, it ignores it. So any arguments passed as an array reference must be in the first array. Seems like it almost should want to do recursion for the current type (column data, table row, whatever), for each hash/array ref it sees. Then you could do something like this:
      $q->td (['two'], [{-colspan=>'2'}, ['2', 'II']])
      and generate the type of output kudra is looking for.
      #!/usr/local/bin/perl -w use strict; use CGI::Pretty qw/:standard/; { my $q = new CGI; print $q->table({-border=>'1', -cellpadding=>'2', -cellspacing=>'2 +'}, Tr({-align=>'LEFT', -valign=>'CENTER'}, [ $q->td(['one', '1', 'I']), $q->td(['two']), $q->td({-colspan=>'2'}, ['2']), $q->td({-colspan=>'2'}, ['three', 'III']), $q->td({-colspan=>'3'}, ['four']), ])); }
      <TABLE CELLPADDING="2" CELLSPACING="2" BORDER="1"> <TR VALIGN="CENTER" ALIGN="LEFT"> <TD> one </TD> <TD> 1 </TD> <TD> I </TD> </TR> <TR VALIGN="CENTER" ALIGN="LEFT"> <TD> two </TD> </TR> <TR VALIGN="CENTER" ALIGN="LEFT"> <TD COLSPAN="2"> 2 </TD> </TR> <TR VALIGN="CENTER" ALIGN="LEFT"> <TD COLSPAN="2"> three </TD> <TD COLSPAN="2"> III </TD> </TR> <TR VALIGN="CENTER" ALIGN="LEFT"> <TD COLSPAN="3"> four </TD> </TR> </TABLE>


      --Chris

      e-mail jcwren
(Ovid) RE: Mid-row colspans with CGI.pm
by Ovid (Cardinal) on Oct 02, 2000 at 21:44 UTC
    I've been working on this problem for a while and I can't solve it. In fact, I'm not sure there is a way around it except use alternatives to CGI.pm's HTML generating methods. The simplest form of the problem is as follows:
    print $q->td(['one', '1', 'I']);
    The above line generates the following:
    <TD>one</TD> <TD>1</TD> <TD>I</TD>
    The problem: how does one take one of those <TD> tags and turn it into the following?
    <TD COLSPAN="2">...</TD>
    The following doesn't work:
    $q->td([ 'two' ]), $q->td({-colspan=>'2'}, [ '2' ]),
    That's because CGI insists upon treating each of these as a separate row, thus closing the <TR> tags around them. To solve this, we need to do all of it within a single td(...) call.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

Re: Mid-row colspans with CGI.pm
by merlyn (Sage) on Oct 03, 2000 at 09:26 UTC
    I hate $q->... when using CGI.pm, so I'd write that whole thing as:
    use CGI::Pretty qw(:standard); print table({border=>1, cellspacing=>2, cellpadding=>2}, Tr({align=>'left', valign=>'center'}, [ td([qw(one 1 I)]), td([qw(one 1 I)]), td('two').td({colspan=>2}, 2), td({colspan=>2}, 'three').td('III'), td({colspan=>3}, 'four'), ]));
    Just remember... if you don't have more than one element, don't use an arrayref below a shortcut. It merely gets confusing. {grin}

    And don't colspan for more columns than you have...

    -- Randal L. Schwartz, Perl hacker

      In the reletively short time I've been using CGI.pm, I've done it sans $q->, like merlyn shows above.   Are there situations with CGI.pm where the extra typing of $q-> provides any benefit or advantage?
          cheers,
          ybiC

        If you use other modules along with CGI.pm and any of those other modules export a function whose name is the same as an HTML tag name, then you'll have to have either CGI.pm or that other module not export that particular function. The object notation allows CGI.pm to not export tons of symbols while providing you an easy way to get to all of the CGI routines.

        You could probably also use CGI::td() to similar effect, though that may raise issues of which sub-module each routine comes from -- since I haven't see this documented I wouldn't consider it supported and so wouldn't recommend it. I just mention this because it is the other standard way to avoid collisions in your global namespace.

        Usually object notation is a good thing for Perl modules because it allows you to not pollute your global name space with lots of imported functions that may clash as you use more modules. It is also good because it usually gets the module author to allow multiple simultaneous uses of that module with different configurations.

        I find it hard to image a case where one script would need more than one CGI object. So it is just a matter of trade-off of not having to type $q-> vs. not having your global namespace "polluted" with tons of short names.

                - tye (but my friends call me "Tye")
        I like the $q->x() OO style syntax :-), looks nicer, and I dont use the $q->hr, $q->table HTML tags.
        Thats the reason I use CGI that way, on systems (Intranet, low volume) where you can ignore performance to a point.

        Lincoln Stein himself uses the functional interface in all his training examples. I can see three times to use the object interface (mind you I just woke up five minutes ago {grin}):
        • When I'm unsure if saying use CGI qw(:standard) will collide with existing subroutines, such as grafting a CGI front onto some previous command-line program
        • In a mod_perl environment, to keep from creating 240 stabs (as we discussed earlier in a thread about CGI.pm's performance) in each Apache::Registry-managed handler
        • When I want to have more than one CGI object in a single program (to save and restore parameters to persistant storage, for example)
        Other than that, use the functional form! Much less typing.

        -- Randal L. Schwartz, Perl hacker

Re: Mid-row colspans with CGI.pm
by runrig (Abbot) on Oct 02, 2000 at 22:02 UTC
    Not a real clean answer, but here's something:
    my $q = CGI->new; my $str = $q->table({-border=>'1', -cellpadding=>'2', -cellspacing=>'2 +'}, $q->Tr({-align=>'LEFT', -valign=>'CENTER'}, [ $q->td(['one', '1', 'I']), $q->td( {-junk=>'', -colspan=>'2'}, [ 'two', '2' ] ), $q->td({-colspan=>'2'}, ['three', 'III']), $q->td({-colspan=>'3'}, ['four']), ])); $str =~ s/(?<=<td)[^>]*junk[^>]*//i; $str =~ s/junk[^>\s]*//i;
    Update: Ignore this, good answer below