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

Hi all, I am trying to execute code that is stored as a text field in a database. The code needs to know which class has called it. This is so it can access instance methods and variables within the calling class, mainly so it can update itself within the DB. When I use eval parse the string it has no class or super class. How can I tell it which class it belongs to?

Replies are listed 'Best First'.
Re (tilly) 1: Inheritance Query
by tilly (Archbishop) on Jan 31, 2002 at 17:31 UTC
    You can find out who called you with caller.

    However your design is sounding rather complex to me. Can you explain what your overall design is? (My main concern is that code which is stored with no class attached could wind up modifying arbitrary classes in an uncontrolled and impossible to debug manner.)

      hi, I am building an email system that works from an Agenda (good old AI style) set up in MySQL. Each entry has an action associated with it that is triggered on a certain day at a certain time eg. Send out X number of mail to X people and update DB to set reminder time for the next set of mails to go out. The system has to be autonomous and capable of updating itself. This code is run through an try{}catch eval block, errors are logged and the system doesn't fall over ;o|Code within an eval block (that isn't an anonymous subroutine) isn't attached to any class. Even when I use caller it returns nothing. It's really annoying... The whole system is a big Web based corporate doodad and I am programming the infrastructure. This is the only bit we are using Perl for, and I must admit I am something of a newbie to Perl :o)
        Insert a package statement within the evaled code and that will associate it with a class, and make it callable by objects in that class. That knowledge will, of course, need to be combined with some logic about what class each bit of code belongs in, but that is another story.

        When you say that you are a bit of a newbie to Perl, are you familiar with how Perl's OO model works? (eg as described by perlobj and perltoot...) If not, then learning that would be a good step.