in reply to Re^2: Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
in thread Assigning to the CODE slot of a GLOB whose REF is held in a lexical?

That's because it's a glob reference, not a code reference:

sub foo { "foo" } $fooref = \*foo; print *$fooref{CODE}->();
  • Comment on Re^3: Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
  • Download Code

Replies are listed 'Best First'.
Re^4: Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
by ikegami (Patriarch) on Nov 16, 2004 at 07:42 UTC

    oops, right, but it doesn't work for another reason:

    use strict; use warnings; my $bar = 1; my $barref = \*bar; *$barref = sub { "bar" }; print &{ *$barref }(), $/; print ${ *$barref }, $/; # XXX undefined, should be 1. print *$barref{CODE}->(), $/; print ${*$barref{SCALAR}}, $/; # XXX undefined, should be 1.

      Scalar variables don't live on the symbol table. This works as advertised:

      use strict; use warnings; our $bar = 1; # note the package variable my $barref = \*bar; *$barref = sub { "bar" }; print &{ *$barref }(), $/; print ${ *$barref }, $/; print *$barref{CODE}->(), $/; print ${*$barref{SCALAR}}, $/;

      Update: no trouble at all. Hopefully it's a "learning experience." :-)

        I'll take that as a hint that I should be in bed. Sorry for the trouble!