in reply to Assigning to the CODE slot of a GLOB whose REF is held in a lexical?

Maybe I'm missing something, but this Works For Me:

sub foo { "foo" } $fooref = \*foo; print *bar{SCALAR}; *bar = *$fooref{CODE}; print *bar{SCALAR}; print *foo{CODE}; print *bar{CODE};

Update: I just noticed you want to assign to a globref. This works too:

sub foo { "foo" } $fooref = \*foo; $bar = 1; $barref = \*bar; print *bar{SCALAR}; *$barref = *foo{CODE}; print *bar{SCALAR}; print *foo{CODE}; print *bar{CODE};

Another Update: assigning an anonymous coderef to the glob ref doesn't overwrite the scalar slot of the glob for me either:

$bar = 1; $barref = \*bar; print *bar{SCALAR}; *$barref = sub { "bar" }; print *bar{SCALAR};

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

    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.

      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.