Thanks guys. You poked me in the right direction.
I knew I had this work before. I turns out that if the base GLOB has something assigned to the CODE slot before you take the reference to it, then you can use any of:
And they all work.
But if the original GLOB has nothing assigned to the code slot, nothing works. Seems like a bug to me?
#! perl -slw use strict; no warnings 'uninitialized'; ## suppress expected warnings ## ASSIGN TO CODE TO THE GLOB FIRST AND... *GLOB = sub { "Anon 1: $_[ 0 ]" }; my $glob = do{ local *GLOB = sub { "Anon 2: $_[ 0 ]" }; \*GLOB }; print "\$glob = ", $glob; print "$_ => ", *$glob{ $_ }, ' => ', $_ eq 'SCALAR' ? ${ *$glob } : $_ eq 'ARRAY' ? join ', ', @{ *$glob } : $_ eq 'HASH' ? join ', ', %{ *$glob } : $_ eq 'CODE' ? &{ *$glob } : 'n/a' for qw[ SCALAR ARRAY HASH CODE IO FORMAT ]; print "\n-------\n\n Now assign some values to the slots\n"; ${ *$glob } = 'A scalar'; @{ *$glob } = qw[ an array ]; %{ *$glob } = qw[ This is a hash ]; print $glob; print "$_ => ", *$glob{ $_ }, ' => ', $_ eq 'SCALAR' ? ${ *$glob } : $_ eq 'ARRAY' ? join ', ', @{ *$glob } : $_ eq 'HASH' ? join ', ', %{ *$glob } : $_ eq 'CODE' ? &{ *$glob } : 'n/a' for qw[ SCALAR ARRAY HASH CODE IO FORMAT ]; ## THIS WORKS NOW *{ *$glob } = sub{ "Anon 4: $_[ 0 ]" }; print "\n\nAfter \*{ *\$glob } = sub{ ... }: ", $glob; print $glob; print "$_ => ", *$glob{ $_ }, ' => ', $_ eq 'SCALAR' ? ${ *$glob } : $_ eq 'ARRAY' ? join ', ', @{ *$glob } : $_ eq 'HASH' ? join ', ', %{ *$glob } : $_ eq 'CODE' ? &{ *$glob } : 'n/a' for qw[ SCALAR ARRAY HASH CODE IO FORMAT ]; ## SO DOES THIS *$glob = sub{ "Anon 5: $_[ 0 ]"; }; print "\n\nAfter *\$glob = sub{ ... }: ", $glob; print $glob; print "$_ => ", *$glob{ $_ }, ' => ', $_ eq 'SCALAR' ? ${ *$glob } : $_ eq 'ARRAY' ? join ', ', @{ *$glob } : $_ eq 'HASH' ? join ', ', %{ *$glob } : $_ eq 'CODE' ? &{ *$glob } : 'n/a' for qw[ SCALAR ARRAY HASH CODE IO FORMAT ]; ## AND SO DOES THIS! *{ $glob } = sub{ "Anon 6: $_[ 0 ]"; }; print "\n\nAfter \*{ \$glob } = sub{ ... }: ", $glob; print $glob; print "$_ => ", *$glob{ $_ }, ' => ', $_ eq 'SCALAR' ? ${ *$glob } : $_ eq 'ARRAY' ? join ', ', @{ *$glob } : $_ eq 'HASH' ? join ', ', %{ *$glob } : $_ eq 'CODE' ? &{ *$glob } : 'n/a' for qw[ SCALAR ARRAY HASH CODE IO FORMAT ]; __END__ [17:06:27.35] P:\test>globs $glob = GLOB(0x1824398) SCALAR => SCALAR(0x182435c) => ARRAY => => HASH => => CODE => CODE(0x1824368) => Anon 1: IO => => n/a FORMAT => => n/a ------- Now assign some values to the slots GLOB(0x1824398) SCALAR => SCALAR(0x182435c) => A scalar ARRAY => ARRAY(0x224fe4) => an, array HASH => HASH(0x224ff0) => a, hash, This, is CODE => CODE(0x1824368) => Anon 1: IO => => n/a FORMAT => => n/a After *{ *$glob } = sub{ ... }: GLOB(0x1824398) GLOB(0x1824398) SCALAR => SCALAR(0x182435c) => A scalar ARRAY => ARRAY(0x224fe4) => an, array HASH => HASH(0x224ff0) => a, hash, This, is CODE => CODE(0x184d4dc) => Anon 4: IO => => n/a FORMAT => => n/a After *$glob = sub{ ... }: GLOB(0x1824398) GLOB(0x1824398) SCALAR => SCALAR(0x182435c) => A scalar ARRAY => ARRAY(0x224fe4) => an, array HASH => HASH(0x224ff0) => a, hash, This, is CODE => CODE(0x184d668) => Anon 5: IO => => n/a FORMAT => => n/a After *{ $glob } = sub{ ... }: GLOB(0x1824398) GLOB(0x1824398) SCALAR => SCALAR(0x182435c) => A scalar ARRAY => ARRAY(0x224fe4) => an, array HASH => HASH(0x224ff0) => a, hash, This, is CODE => CODE(0x184d7f4) => Anon 6: IO => => n/a FORMAT => => n/a
In reply to Re: Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
by BrowserUk
in thread Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |