I was wondering if it is possible to have a subroutine stored as a string and then used as a normal subroutine within a program at run-time.

Have you given sufficient consideration as to why you want to do that? Have you considered any alternatives? If the answer is 'yes' and 'yes' or if you are determined to go this route, you may stop reading this now.

Pulling and executing code directly from a database like that is bound to cause maintenance headaches. It might be more advantageous for you to specify a 'skeleton' subroutine in your code and 'clone' it if you want to define similar subroutines at a later time. The benefit of this approach is that you minimize the domain of valid inputs to your code Basic Coding Tips: Parsimonious Parameterization, and thereby simplify debugging and interpreting the acceptable range of outputs. Even inventing your own 'mini language' would be better than simply pulling in subroutines as strings, because you are basically saying you want your inputs to be any concievable set of perl instructions ... that can't be good.

To translate all this abstract talk into a perl code example, here is an example that creates numerous subroutines that get 'cloned' into your perl code at runtime.

### begin_: init perl use strict; use warnings; my $app; ### begin_: call our 'Clone Generator' CloneSubroutines(); ### begin_: print some results print $app->{show_tag_a}('alpha') . qq'\n'; print $app->{show_tag_b}('bravo') . qq'\n'; print $app->{show_tag_c}('charlie') . qq'\n'; print $app->{show_tag_g}('delta') . qq'\n'; ### --- print &main::show_tag_e('echo'); print show_tag_f('foxtrot'); ### begin_: declare our 'Clone Generator' subroutine sub CloneSubroutines { ### simple example; create different types ### of 'tags' using perl data variables $app->{tag_a} = ['<a>', '</a>']; $app->{tag_b} = ['<b>', '</b>']; $app->{tag_c} = ['<c>', '</c>']; $app->{tag_d} = ['<d>', '</d>']; $app->{tag_e} = ['<e>', '</e>']; $app->{tag_f} = ['<f>', '</f>']; $app->{tag_g} = ['<g>', '</g>']; ### auto-generate (Clone) numerous subroutines ### based on a single subroutine template ### BUGNAG: must use no strict 'refs' pragma for my $tag (sort keys %{$app}) { no strict 'refs'; my $functionName = qq'show_$tag'; ### clone subroutines $app->{$functionName} = sub { my $beg = $app->{$tag}[0]; my $end = $app->{$tag}[1]; my $mid = shift || ''; my $strOut = ''; $strOut = qq'$beg$mid$end'; return $strOut; }; ### copy subroutines *$functionName = $app->{$functionName}; } }###end_sub 1; __END__ <a>alpha</a> <b>bravo</b> <c>charlie</c> <g>delta</g> <e>echo</e><f>foxtrot</f>

In reply to Re: Creating subroutines on the fly by dimar
in thread Creating subroutines on the fly by pander

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.