in reply to How to store CODE ref

Simple answer is go with mod_perl as mentioned before.

Longer answer: This is pretty much completely untested, but you could overload strigification and use a tied scalar which keeps the coderef and the original code together. Then you can do something sort of like:

tie $somecode, CodeNRef => qq{print "This is my code\n"}; $somecode->( ); print tied $somecode;

Where the hypthetical CodeNRef package would look something like:

package CodeNRef; use overload q{""} => \&print_code; sub TIESCALAR { my( $class, $code ) = @_; my $ref = eval qq{ sub { $code } }; return bless { code => $code, compiled => $ref }, $class; } sub print_code { shift()->{code} } sub FETCH { my $self = shift; return $self->{compiled} } 1;

Update: Feh, who needs to play around with tie'd stuff. Just overload &{}.

package CodeNRef; use overload q{""} => \&print_code, q{&{}} => \&call; sub new { my( $class, $code ) = @_; my $ref = eval qq{ sub { $code } }; return bless { code => $code, compiled => $ref }, $class; } sub print_code { shift()->{code} } sub call { shift()->{compiled} } 1;