http://www.perlmonks.org?node_id=1051870


in reply to Re^5: CGI::Ajax No head/html tags, nowhere to insert. Returning javascript anyway.
in thread CGI::Ajax No head/html tags, nowhere to insert. Returning javascript anyway.

Hi, Anonymous Monk Thanks for your calm advice and code. Below is code which worked.
$html .= $cgi->div({-id=>'container'}, $cgi->img({src=>'images/pic.png', height=>'100px', + width=>'150px'}), ( (($session->is_expired) || ($session->is_empty)) ? ($cgi->p("false")) :($cgi->p("true")) + ), $cgi->div( {-id=>'menu_bar'}, # rest of html

And here is code, which didn't

$html .= $cgi->div({-id=>'container'}, $cgi->img({src=>'images/pic.png', height=>'100px', + width=>'150px'}), ( (while ($num <=5) {($cgi->p($num)) ($num++)}) ), $cgi->div( {-id=>'menu_bar'},

What I did wrong in 'while' case. I went through links, provided by you. So here $html .= $cgi->div#rest of code, is one big statement and rest of part on right side are expressions.

But, could not grasped the concept and structure behind parenthesis well.

And is it possible to use other Perl blocks in this fashion.?

Replies are listed 'Best First'.
Re^7: CGI::Ajax No head/html tags, nowhere to insert. Returning javascript anyway.
by Anonymous Monk on Sep 02, 2013 at 02:33 UTC

    Whoa, that's creative; I don't know how I managed to communicate that, but I wasn't trying to communicate that

    If you're trying to learn new syntax, esp if you're working on something already , start a new file (start 10 new files actually), and type it :) that way a year later, you have code you wrote with your own fingertips showing you whats important about that new syntax -- stuff like that you remember :) well I remember

    Hopefully this is clearer (I inglish you not my strong porcupine suit )

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd pp /; my $str = 'h'; dd $str; ## "h" $str .= 'i'; dd $str; ## "hi" ## one way to write this mini-if aka ternary #~ $str .= 1 > 2 ? '; uh oh 1>2' : '; ok 1<2' ; ## same exact thing, just with more whitespace , easier to read #~ $str .= 1 > 2 #~ ? '; uh oh 1>2' #~ : '; ok 1<2' #~ ;;; ## once again the same exact thing (this time for real) ## the parens provide visual disambiguation ## and not important in this example, also disambiguation for operator + precedence ## $str .= ( 1 > 2 ) ? ( '; uh oh 1>2' ) : ('; ok 1<2'); dd $str; ## "hi; ok 1<2" $str .= do { my $foo = 0; for(1..3){ $foo .= $_; } $foo; ## last statement is like "return $foo;" }; # end of do dd $str; ## "hi; ok 1<20123" __END__ "h" "hi" "hi; ok 1<2" "hi; ok 1<20123"

    porcupine suit aka "armouire" -- ah gess, the pans are mah favreeeet :D

      ## $str .= ( 1 > 2 ) ? ( '; uh oh 1>2' ) : ('; ok 1<2'); ## is the same as like if( 1 > 2 ) { $str .= ( '; uh oh 1>2' ); } else { $str .= ( '; ok 1<2' ); }
Re^7: CGI::Ajax No head/html tags, nowhere to insert. Returning javascript anyway.
by poj (Abbot) on Sep 02, 2013 at 07:13 UTC
    Use map
    $html .= $cgi->div({-id=>'container'}, $cgi->img({src=>'images/pic.png', height=>'100px', width=>'150px'}), map{$cgi->p($_)}($num..5), $cgi->div({-id=>'menu_bar'},
    poj

      Hi monks,

      Anonymous monk, and poj, that was helpful, and below is code, which worked

      $html .= $cgi->div({-id=>'container'}, $cgi->img({src=>'images/pic.png', height=>'100px', + width=>'150px'}), ( do { for(1..3){ $foo .= $_ } $foo ## last statement is like "return $foo;" } # end of do ), $cgi->div(

      I took some time to meditate (or staring ;-)), on Perl Documentation (perldoc perl), and things started to make sense.

      Thanks again! and I am back to meditation ;-)