in reply to Templating suggestions for code generation

... and of course, for the fans of that curious and little-known templating language "perl" ... (cheatsheet version, "readmore" section has the full version)

### -------------------------- .q© // BEGIN© .sub{(@{$oDt})?" switch(x){" .sLp($oDt,sub{my $sBrk=($iCt++<$#{$oDt})?'break;':'';" case $_: // code for $_ $sBrk" })." }" :''}->(). q© // END ©; ### --------------------------

Complete version ...

### begin_: init perl use strict; use warnings; my $oTest = []; #$oTest = [qw(foo bar baz)]; #$oTest = [qw(foo)]; #$oTest = [qw()]; ### begin_: display output print CodeGen($oTest); ### begin_: template function sub CodeGen{ my $sOut = ''; my $oDt = shift || []; my $iCt = 0; $sOut .= "" ### -------------------------- ### -------------------------- .q© // BEGIN© .sub{(@{$oDt})?" switch(x){" .sLp($oDt,sub{my $sBrk=($iCt++ <$#{$oDt})?'break;':'';" case $_: // code for $_ $sBrk" })." }" :''}->(). q© // END ©; ### -------------------------- ### -------------------------- $sOut =~ s/^\n//; return $sOut; } ### begin_: helper functions sub sLp { join"",map{$_[1]->();}(@{$_[0]}) }###end_sub 1; __END__ ---- // BEGIN // END ---- // BEGIN switch(x){ case foo: // code for foo } // END ---- // BEGIN switch(x){ case foo: // code for foo break; case bar: // code for bar break; case baz: // code for baz } // END ----