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

At work there is this entire testing system that is written in Perl and I am not exactly clear on how/what it does.. so if this question does not appear to be a Perl issue, it could very well be something to do with the testing system. Having said that, here is my question:

I have a series of functions that are called, and inside each of these functions an object is created from a common class. I use a reference to that object only inside the function where it is created.

When each of the functions are executed, the first one runs without any warnings, while all subsequent creations using the same class results in warnings about functions being re-defined.

And that is ok, I guess, since each creation of the object calls a series of eval statements that create a set of functions for setting and getting object values. But then again its not ok to me because I am confused as to why the pre-existing evals are still remembered. I thought that the evals were specific to that objects methods and when each function exited, the object as well as its dynamically created functions would be lost. Obviously this is not the case since the solution to this problem was to have a use statement that passed values to Class:MethodMaker.

But this now brings me to my question. What happened inside Perl that made the evals for the one object create entries for the entire namespace of that class? How does Perl handle evals that dynamically create functions, i.e. what are the steps that are taken to implement that. Or, where is there an existing explanation into the relative working parts of Perl?

Thank you for your attention and I understand if this explanation is lacking. Please feel free to ask me to clarify.

the_Don
...making offers others can't rufuse.

Replies are listed 'Best First'.
Re: Redefined Methods.. ok, Why?
by perrin (Chancellor) on Feb 03, 2003 at 19:52 UTC
    Named subroutines belong to a namespace. When you create a subroutine with eval, it is added to a namespace for the life of the program, unless something else removes it. Perl's OO system revolves around telling it which namespace it should find its methods in. It does not attach methods to individual objects.

    You should change your code so that it only creates these subs the first time. Otherwise you are slowing down your code. A common approach is to put this kind of setup work in a BEGIN block.

Re: Redefined Methods.. ok, Why?
by dragonchild (Archbishop) on Feb 03, 2003 at 20:36 UTC
    You're going to want to add a check similar to:
    *{"${class}::${sub_name}"} = $class->make_accessor( $index + $offset, $attr_name, ) unless defined &{"${class}::${sub_name}"};
    where make_accessor() returns a code-reference for an accessor method that accesses $attr_name which is located at a specific index ($index + $offset) in an array-based object.

    The important part is the unless defined part.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.