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

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 ...

Replies are listed 'Best First'.
Re^3: Using SUPER in dynamically generated subs
by ikegami (Patriarch) on Nov 13, 2006 at 17:45 UTC

    ( 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

        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 $@; ...