in reply to Using SUPER in dynamically generated subs

To fix your problem, execute the eval when calling install_sub, not in the function called by install_sub. It's even more efficient (since it doesn't call eval everytime the built sub is called)!
... my ($class, $alias_for) = @_; my $sub = eval "package $class;" . <<'__EOI__'; sub { my ($class, $params) = @_; do_something_with($alias_for); return $class->SUPER::find_ids($params); } __EOI__ die $@ if $@; install_sub( $class, 'find_ids', $sub ); ...

Replies are listed 'Best First'.
Re^2: Using SUPER in dynamically generated subs
by clinton (Priest) on Nov 13, 2006 at 17:35 UTC
    When I tried this

    my ($class,$alias_for) = @_; eval "package $class;"<<'SUB'; sub find_ids { my ($class,$params) = @_; # Do something with $alias_for return SUPER::find_ids->($class,\%params); }); SUB

    it gives me the message:

    Variable "$alias_for" will not stay shared at ...

      ( Sorry, this post is completely wrong. )

      That's a different problem, relating to threads and shared variables.

      Is $alias_for a shared variable? I'm no threading expert, but it makes sense that captures do not remain shared. Perhaps you want to capture a reference to the shared variable instead of capturing the shared variable itself.

      my $alias_for_ref = \$alias_for; eval "package $class;"<<'SUB'; sub find_ids { my ($class,$params) = @_; # Do something with ${$alias_for_ref} return SUPER::find_ids->($class,\%params); }); SUB
        No - it has nothing to do with threads. It means that the deep binding, which I was relying on, has been broken : see Variable will not stay shared