use strict; use warnings; my $combined = ''; my ( $a, $b, $c ) = ( 1, 0, 1 ); eval qq(if ( \$$_ ) { \$combined .= "$_=\$$_ " }) for qw( a b c ); print "$combined\n"; __END__ a=1 c=1 #### 'if ( $a ) { $combined .= "a=$a " }' #### my $template = 'if ( $%s ) { $combined .= "%s=$%s " }'; eval sprintf $template, $_, $_, $_ for qw( a b c ); #### '$combined .= "%s=$%s " if $%s' #### '$%s and $combined .= "%s=$%s "'