in reply to Re: 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?

Doesn't for me:

sub foo { "foo" } $fooref = \*foo; &$fooref();

gives

Not a CODE reference at testing.pl line 3.

"Another Update" doesn't work for either:

$bar = 1; $barref = \*bar; *$barref = sub { "bar" }; &$barref();

gives

Not a CODE reference at testing.pl line 4.

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

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

    sub foo { "foo" } $fooref = \*foo; print *$fooref{CODE}->();

      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." :-)