in reply to Auto-generated constructors not finding correct SUPER?

SUPER is relative to __PACKAGE__, and the __PACKAGE__ of your constructor is Bar, not Foo_child. Be sure you have "package Foo_child" before you use SUPER there.

So, for example, your code doesn't work:

package Wrong; *{'Right::new'} = sub { ... SUPER ... };
nor does this:
package Wrong; sub Right::new { ... SUPER ... }
but this does:
package Right; sub new { ... SUPER ... }
as does this:
package Wrong; sub Right::new { package Right; ... SUPER ... }

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

Replies are listed 'Best First'.
Re^2: Auto-generated constructors not finding correct SUPER?
by dragonchild (Archbishop) on Jan 10, 2005 at 13:53 UTC
    So, given that, how would I make the following code work in package Right? My problem is that I need to create constructors that are closures ...
    package Wrong; sub builder { my $pkg = shift; my @args = ( # A bunch of random stuff here, including objects ); { no strict 'refs'; *{ $pkg . '::new' } = sub { (shift)->SUPER::new( @args ); }; } }

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        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