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

Is it possible to use an if statement within a cgi.pm table? For example:
table ( if ($x) { Tr ( td ("1"), td ("2"), ) } else { Tr ( td ("3"), td ("4"), ) } );
I get a syntax error near the if statement. Any work arounds would also be appreciated.

Thanks for your help!

Replies are listed 'Best First'.
(jeffa) Re: If statements with CGI.pm tables
by jeffa (Bishop) on May 08, 2002 at 15:30 UTC
    Try the ternery operator instead:
    print table ( $x ? Tr ( td ("1"), td ("2"), ) : Tr ( td ("3"), td ("4"), ) );

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: If statements with CGI.pm tables
by mephit (Scribe) on May 08, 2002 at 18:57 UTC
    print start_table({-border => '', -width => "100%"}), ( $foo ? Tr(td('foo is set'), td('foo is still set')) : Tr(td('foo is not set'), td('foo is still not set')) ), end_table;
    That works. (I initially tried putting a bare block inside the print statement, but it looks like perl saw it as an anonymous hash, instead.) Or you can just put your conditional outside of any print statements:
    print start_table({-border => '', -width => "100%"}); if ($foo) { print Tr(td('foo is set'), td('foo is set, still')); } else { print Tr(td('foo is not set'), td('foo is not set, yet')); } print end_table;