http://qs321.pair.com?node_id=1095332


in reply to Re: a perl code syntax help
in thread a perl code syntax help

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 ?

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

    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.

Re^3: a perl code syntax help
by hexcoder (Deacon) on Jul 28, 2014 at 21:08 UTC
    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).