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:

  1.  *{ *$glob } = sub{ "Anon 4: $_[ 0 ]" };
  2.  *$glob = sub{ "Anon 5: $_[ 0 ]"; };
  3.  *{ $glob } = sub{ "Anon 6: $_[ 0 ]"; };

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

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.