in reply to Re^2: Auto-generated constructors not finding correct SUPER?
in thread Auto-generated constructors not finding correct SUPER?

You've got to string-eval a "package $pkg" ahead of the definition. Be very careful when you do that that you don't accidentally expand the other variables in place.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re^3: Auto-generated constructors not finding correct SUPER?

Replies are listed 'Best First'.
Re^4: Auto-generated constructors not finding correct SUPER?
by stvn (Monsignor) on Jan 10, 2005 at 17:39 UTC
    merlyn

    I am confused by your answer, when I try this:

    { eval "package $pkg;"; no strict 'refs'; *{$pkg . '::new'} = sub { (shift)->SUPER::new(%{$fields}) }; }
    I get the same error as before. And when I try this:
    { no strict 'refs'; *{$pkg . '::new'} = sub { eval "package $pkg;"; (shift)->SUPER::new(%{$fields}) }; }
    I get the same error as well. It would seem to me that SUPER is determined at compile time, is that true?

    Does the whole subroutine need to be eval-ed?

    -stvn
        merlyn,

        Thanks for the quick response. I tried this:

        eval "package $pkg; sub new { (shift)->SUPER::new( \%\$fields ); }"
        and got this:
        Variable "$fields" will not stay shared at (eval 32) line 1.
        Although the the 300+ tests passed, and if $fields was somehow lost, they would not have passed. Is this warning something I should worry about? Any idea as to what context $fields is not staying shared within?

        -stvn

        dragonchild figured it out, this code works.

        eval "package $pkg; *new = sub { (shift)->SUPER::new( \%\$fields ); }"
        It avoids the warnings, and all the tests pass. Thanks for the help.

        -stvn