Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Conditional statements in CGI/Div

by slurch901 (Initiate)
on Jan 13, 2013 at 17:41 UTC ( #1013116=perlquestion: print w/ replies, xml ) Need Help??
slurch901 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm having a problem inserting conditional statements into already open
using CGI. Here is a extremely basic version of the code, but if I can get this to work, I can apply it to the rest.
#!/usr/bin/perl -w use CGI; use CGI ":standard"; $cgi = new CGI; print $cgi->header(), $cgi->start_html(), $cgi->div({-id=>pagewrap}, if($index==0) { $cgi->p("Hello"), } ), $cgi->end_html();
It works without any type of conditional statement, but as its in a
it seems to cause problems. Cheers.

Comment on Conditional statements in CGI/Div
Download Code
Re: Conditional statements in CGI/Div
by golux (Pilgrim) on Jan 13, 2013 at 18:07 UTC
    Hi slurch901,

    Instead of putting the conditional inside the div(...) call, put it immediately afterwards (and start a separate print for the final $cgi->end_html()):

    #!/usr/bin/perl -w use CGI; use CGI ":standard"; use CGI::Carp qw{ fatalsToBrowser }; $index = 0; $cgi = new CGI; print $cgi->header(), $cgi->start_html(), $cgi->div({-id=>pagewrap}); if($index==0) { print $cgi->p("Hello"), } print $cgi->end_html();

    Notice that the code above has the very useful:

    use CGI::Carp qw{ fatalsToBrowser };
    This will cause many errors to display in the webpage itself, which is how I was able to see the error in my browser:
    syntax error at /var/www/html/index.cgi line 10, near "if" Execution of /var/www/html/index.cgi aborted due to compilation errors +.
    say  substr+lc crypt(qw $i3 SI$),4,5
      Thanks for the quick reply and the Carp statement, but if I do it the way you have done then $cgi->p("Hello") doesn't inherit the pagewrap div from the css as it's closing too early.

        Shouldn't you stop using CGI anyway? If you use something with MVC as CGI::Application or Catalyst you wouldn't have to bother with those details.

        Alceu Rodrigues de Freitas Junior
        ---------------------------------
        "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill
Re: Conditional statements in CGI/Div
by Khen1950fx (Canon) on Jan 13, 2013 at 21:17 UTC
    I condensed golux's script by 4 lines. Hopefully, this might be a little bit easier to read.
    #!/usr/bin/perl -l use strict; use warnings; use CGI qw/:standard/; my $index = 0; if($index == 0) { my $cgi = new CGI; print $cgi->header(), $cgi->start_html(), $cgi->div({-id => 'pagewrap'}, "Hello"), $cgi->end_html(); }
    Update: Condensed even more:
    #!/usr/bin/perl -l use strict; use warnings; use CGI qw/:standard/; my $index = 0; if($index == 0) { my $cgi = CGI->new; print header, start_html, div({-id=>'pagewrap'}, "Hello"), end_html; }
Re: Conditional statements in CGI/Div
by NetWallah (Abbot) on Jan 13, 2013 at 22:24 UTC
    Two possible (untested) options:
    use CGI qw/:standard *div/; my $cgi = new CGI; print $cgi->header(), $cgi->start_html(), $cgi->start_div({-id=>pagewrap}); # NOTICE ** START_DIV if($index==0) { print $cgi->p("Hello"); } print $cgi->end_div(), $cgi->end_html();
    Or, use the ternary form:
    use CGI ":standard"; my $cgi = new CGI; print $cgi->header(), $cgi->start_html(), $cgi->div({-id=>pagewrap}, $index==0 ? $cgi->p("Hello") : "" ), $cgi->end_html();

                 Most people believe that if it ain't broke, don't fix it.
            Engineers believe that if it ain't broke, it doesn't have enough features yet.

Re: Conditional statements in CGI/Div
by blue_cowdawg (Monsignor) on Jan 14, 2013 at 14:27 UTC

    Here's what you need:

    $cgi->div({-id=>pagewrap}, ($index==0 ? $cgi->p("Hello") : $cgi->p() ) }
    or something long those lines.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Thanks everyone for your input but the only way I could solve it was instead of using the DIV statement with brackets was to use the alternative.
      print $cgi->start_div({-id=>pagewrap}); if($index==0) {print "Hello";} print $cgi->end_div();
Re: Conditional statements in CGI/Div
by Your Mother (Chancellor) on Jan 27, 2013 at 23:31 UTC

    Your style is all over the place and to do in such a sort space is something of an achievement. :P Please use strict and pick a CGI style. You are importing a ton of functions (":standard") but using it object oriented. There is almost never a reason to use it OOP in a standalone script; only if you need to pass it around libraries or harnesses.

    You can't just drop a post-fix conditional into a print list. You can use the Enterprise operator (see Secret Perl Operators: the boolean list squash operator, x!!), among a couple other tricks though–

    use strict; use CGI ":standard"; my $index = param("index") || 0; print header(), start_html(), div({ -id => "pagewrap" }, ( p("Hello") ) x ! $index, ), end_html();

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013116]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2015-07-04 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls