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

I have a Perl module which writes another Perl script, and wants to have that Perl script call me back (in the creating module). Since I am writing code, the $self I use in my print gets stringified, and of course cannot be called.

Is there a way to destringify the stringified class to be able to call a method therein properly?

TIA

Replies are listed 'Best First'.
Re: Destringify class
by kyle (Abbot) on Aug 12, 2008 at 19:37 UTC

    How are you executing the script that you write? If it's a completely separate process, it can't access the object that created it.

    If you're using do or require, you can make $self a kind of global that the script can access.

    If you fork and execute the separate process, it has access to everything that was there at the time of the fork. This is a slightly modified case of one of the above, but it's important to note that the object in the child will not be the same as the one that wrote the script. Rather, it will be a copy. It can do the same things, but it can't modify the original.

    In answer to the question you asked, have a look at Devel::Pointer.

    For a more detailed answer, I think you'll have to show us some code.

      I am using 'require' to execute the script. The only reason I am doing this is that I need the facilities of Filter::cpp, and it has to run on a file, as far as I can tell. If I could run this internally, it'd be a lot easier.

        Here's a demonstration of what I was writing about:

        use strict; use warnings; package My::Self; sub new { bless $_[1] } package main; my $obj = My::Self->new({ authentic => int rand 1_000_000 }); my $to_be_required = './deleteme.pl'; open my $out_fh, '>', $to_be_required or die "Can't write '$to_be_required': $!"; print {$out_fh} <<"END_OF_SUBSCRIPT"; print "My name is '$to_be_required'\n"; print "I've heard of this self: ", \$main::self_everyone_knows->{authentic}, "\n"; print "Have a nice day!\n"; END_OF_SUBSCRIPT ; close $out_fh; $main::self_everyone_knows = $obj; require $to_be_required; print "Did it know $obj->{authentic}?\n"; unlink $to_be_required or die "Can't unlink '$to_be_required': $!"; __END__ My name is './deleteme.pl' I've heard of this self: 507287 Have a nice day! Did it know 507287?

        Some things to note:

        • This tosses out a warning about $main::self_everyone_knows being used only once.
        • I've set $main::self_everyone_knows after I wrote the file. Its value isn't used when writing the file actually. In the file, there's a literal "$main::self_everyone_knows", but the first line will be "print "My name is './deleteme.pl'\n"". $main::self_everyone_knows just has to have its value before the require.
        • You can do whatever you want to the file between when you write it and when you require it.
Re: Destringify class
by plobsing (Friar) on Aug 12, 2008 at 20:01 UTC
    With caveats similar to kyle's fork example, if a clone is acceptable, you might consider stringifying $self with Data::Dumper and/or overloading $self's stringification so you can easily obtain a clone.
      Or just storing it with DBM::Deep which properly understands Perl objects, works with multiple processes accessing it simultaneously, and has transactions.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?