dsheroh has asked for the wisdom of the Perl Monks concerning the following question:

Is there a standard method that's similar to eval, but just compiles the code and returns a reference which can be stored and used to execute the code (repeatedly) at a later time?

Replies are listed 'Best First'.
Re: eval that doesn't exec?
by chromatic (Archbishop) on May 11, 2002 at 17:28 UTC
    my $subref = eval "sub {\n" . $yourcode . "\n}";

    I always put the newlines in there because I've had comments at the very end mess with the final brace. The first newline isn't particularly necessary, but it's a good visual cue.

Re: eval that doesn't exec?
by Dog and Pony (Priest) on May 11, 2002 at 17:34 UTC
    Well, maybe this is what you want:
    # store a reference to a sub my $coderef = sub { print shift || "Foo"; print "\n"; }; # execute code, with or without arguments $coderef->(); $coderef->('Bar'); $coderef->();
    It also works if you need to eval a string, then you can do like this:
    my $code =<<'CODE'; print shift || "Foo"; print "\n"; CODE my $coderef = eval"sub{$code}";

    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
      chromatic's answer looks like it's probably what I want, but, just to be sure, I'm looking for a way to store code in a database, pull it out as needed, and repeatedly execute it without needing to recompile on each execution.
        Well, maybe you would want to look at B::Bytecode or Storable then. :)
        You have moved into a dark place.
        It is pitch black. You are likely to be eaten by a grue.