Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: a perl code syntax help

by Discipulus (Canon)
on Jul 28, 2014 at 11:09 UTC ( [id://1095330]=note: print w/replies, xml ) Need Help??


in reply to a perl code syntax help

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.

Replies are listed 'Best First'.
Re^2: a perl code syntax help
by tobyink (Canon) on Jul 28, 2014 at 11:25 UTC

    "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.
Re^2: a perl code syntax help
by ghosh123 (Monk) on Jul 28, 2014 at 11:17 UTC

    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).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found