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

I have an object which is created with such attributes as
new ObjectName( code => sub { do some perl stuff with @_ } );
Now the resulting object is complex and large and takes a fair bit of work to put together so what I want to to is be able to save the object to disk.

The problem though (as you can guess) is accessing the code in the coderef. I want to be able to save it in some manner. Now I don't need to keep the perl code itself. If there's some way of storing the bytecode or anything else that I can at least execute once read back from a file, then I'm happy.

Thanks and cheers
Rick

Replies are listed 'Best First'.
Re: Saving code from an anonymous coderef
by liz (Monsignor) on Oct 06, 2003 at 09:22 UTC
    Maybe 5.8.1 holds the answer for you. From perldelta:
    Storable
    Can now store code references (via B::Deparse, so not foolproof).

    So I guess YMMV.

    Liz

      Data::Dumper will use B::Deparse on coderefs if you set $Data::Dumper::Deparse to true.

      YAML will also use B::Deparse if you set $YAML::UseCode to true.

      At least with these you can look over the code B::Deparse gives you.

      --

      flounder

Re: Saving code from an anoymous coderef
by RMGir (Prior) on Oct 06, 2003 at 12:58 UTC
    This isn't quite what you asked, since you wanted to save the byte code. But assuming B::Deparse isn't bulletproof, how about saving the source code instead?
    new ObjectName( codeStr => 'sub { do some perl stuff with @_ }' ); package ObjectName; #... sub new { #...., not forgetting to set $self->{codeStr} from args $self->{code}=eval $self->{codeStr}; } #...
    You'd have to serialize codeStr and then eval it when you load, but you won't be able to escape that anyhow, I think.
    --
    Mike
Re: Saving code from an anonymous coderef
by etcshadow (Priest) on Oct 06, 2003 at 19:52 UTC
    You're just begging for trouble with this, unless you set yourself up a good bit more structure. The problem is that perl "coderef"s are not just references to compiled code. They are closures. In other words, they contain an imutable reference to the context in which they were instantiated. Take the code from one closure, and use it to instantiate another closure in another context, and they will be different things (and potentially behave differently).

    Here's a quick and dirty example (try it!)

    my $x = 0; my $sub1 = sub { print "$x\n" }; my $sub2; { my $x = 10; $sub2 = sub { print "$x\n" }; } &$sub1; &$sub2;

    The output will be:
    0
    10

    even though $sub1 and $sub2 originate from exactly the same code!

    Speaking from experience with similar sorts of situations, what I would recomend is: don't store a coderef... just store the code as a character string, and eval it in context. This gives you dynamic lexical scoping, as opposed to static lexical scoping (closures), which is (possibly) more what you want to happen. Same example, converted:

    my $x = 0; my $sub1 = q{ print "$x\n" }; my $sub2; { my $x = 10; $sub2 = q{ print "$x\n" }; } eval $sub1; die $@ if $@ ne ""; eval $sub2; die $@ if $@ ne "";

    This will give you:
    0
    0

    That is: the same output from the same code, regardless of the context in which you instantiated the code.


    -------
    :Wq
    Not an editor command: Wq