in reply to Re^2: Using SUPER in dynamically generated subs
in thread Using SUPER in dynamically generated subs

( 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

Replies are listed 'Best First'.
Re^4: Using SUPER in dynamically generated subs
by clinton (Priest) on Nov 13, 2006 at 17:53 UTC
    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

      Oops! I was thinking of another warning.

      It turns out the warning is spurious in this case. We want a class's find_ids to refer to $alias_for as it was when find_ids was compiled, which is to say we want $alias_for not to be "shared".

      The warning can be silenced by changing

      sub find_ids {
      to
      *find_ids = sub {

      Tested.

      ... my ($class, $alias_for) = @_; my $sub = eval "package $class;" . <<'__EOI__'; *find_ids = sub { my ($class, $params) = @_; do_something_with($alias_for); return $class->SUPER::find_ids($params); } __EOI__ die $@ if $@; ...
        Thanks ikegami - that works a treat.

        When I benchmarked the three methods, I got this:

        Rate super eval orig super 7794/s -- -88% -93% eval 67568/s 767% -- -35% orig 104167/s 1236% 54% --

        ... where super is using SUPER, eval is ikegami's method, and orig is the one in the original post.

        The eval method would be preferable because it checks the @ISA at runtime, while my method, although faster, would only check @ISA at compile time.

        UPDATE At ikegami's suggestion, I have added the benchmark code: