Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

a perl code syntax help

by ghosh123 (Monk)
on Jul 28, 2014 at 10:58 UTC ( #1095329=perlquestion: print w/replies, xml ) Need Help??

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

Hi monk,
I am working with a Perl which looks like below :

sub display { $self = shift; if(exists $error){ $html .= "<table border=0>"; $html .= "<tr>"; $html .= "<td align=center ><b>".$self-> {error} +."</b></td>"; $html .= "</tr>"; $html .= "</table><br>"; } $html .= <<END; <table border=0> <tr> <th bgcolor="#CCCCEE">Name</th><th>&nbsp; </th> +<th bgcolor="#CCCCEE">Address</th> </tr> <tr align=center> <td bgcolor="#EEEEEE">@{[ $qry->popup_menu(-id=>'ID +', -name=>'PS', -size=>10, -values=>$array_ref, -labels=>$hash_ref, +-onChange=>"fetchData(id,'','','')") ]}</td> </tr> </table> END return $html; }

In the above code, what does this <<END and END mean ?
how can I change the code which is in between that <<END and END, to insert a if-else condition for that popup_menu.

To clarify further, depending on some values passed to that function, I want another popup_menu which will have a onload function instead of onChange.
In this above code, how can I do that by writing a if-else condition.

Replies are listed 'Best First'.
Re: a perl code syntax help
by Discipulus (Abbot) on Jul 28, 2014 at 11:09 UTC
    hello, you are facing a so called here-doc declaration: you can find more on this here

    You cannot insert a conditional statemenent in an such a construction.
    But you can easyly wrap the assignement in a conditional:
    ##not tested if ($a){$html .= <<END; #here case A END } else{$html .= <<END; #here case B or default END }

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      "You cannot insert a conditional statemenent in an such a construction."

      I beg to differ!

      use strict; use warnings; print <<"END" for 1..100; @{[$_%3?():"Fizz",$_%5?():"Buzz",$_%5&&$_%3?$_:()]} END

      For simpler examples, it's even possible to make it sort of legible!

      use strict; use warnings; print <<"END" for 1..100; @{[ ($_ % 2) ? 'Odd' : 'Even' ]} END
        ehehe i had some precognition when i wrote that..
        Better i would say: 'I tried but it was a...' but in Perl many many things are possible!

        Thanks tobyink
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      thanks for your reply and pointer to the doc.
      I just do not want to write the same code again and again, can I write a function and pass 'onload' or 'onChange' in a variable? so that it would resolve the $func value as -onChange or -onload ?

        You could make life much easier for yourself by using a templating system rather than creating HTML in this manner, but you already know that.

        Yes, we can. Like that (untested):
        ... my $trigger = 'onload'; $html .= <<END; ... <td bgcolor="#EEEEEE">@{[ $qry->popup_menu(-id=>'ID +', -name=>'PS', -size=>10, -values=>$array_ref, -labels=>$hash_ref, +-${trigger}=>"fetchData(id,'','','')") ]}</td> </tr> </table> END
        The original code did already interpolate a list expression in the heredoc string (@{[ ... ]} was already present). So you just have to replace the fixed text part (onChange) in the above expression (...) with a string variable (here I used $trigger).
Re: a perl code syntax help
by kcott (Bishop) on Jul 28, 2014 at 11:32 UTC

    G'day ghosh123,

    "In the above code, what does this <<END and END mean ?"

    The construct

    <<END; ... END

    is called a heredoc.

    Simplistically, it's just a string. In perlop, it's introduced in "Quote and Quote-like Operators" with a lot more detail in "Quote-Like Operators".

    "how can I change the code which is in between that <<END and END, to insert a if-else condition for that popup_menu."

    Now that you know that's a string, you probably won't be trying to do that any more. :-)

    I'd suggest something along these lines:

    my $popup_string; if (some_condition()) { $popup_string = ...; } else { $popup_string = ...; } $html .= <<END; ... <td bgcolor="#EEEEEE">$popup_string</td> ... END

    -- Ken

Re: a perl code syntax help (ppi_dumper, wxPPIxregexplain.pl
by Anonymous Monk on Jul 28, 2014 at 20:44 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-05-25 04:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?