I swear I've had this work before, but not today. I have a GLOBREF held in a lexical scalar. I can assign new values to the GLOBs SCALAR, ARRAY & HASH slots, independantly of each other just fine.

Now I need to add or modify the CODE slot, without affecting the existing values in the other slots. I've tried various syntaxes, including some stupid ones, but nothing work?

#! perl -slw use strict; no warnings 'uninitialized'; ## supress expected warnings ## Place a globref into a lexical scalar my $glob = \ do{ local *GLOB; }; print "\$glob = ", $glob; print "$_ => ", *$glob{ $_ }, ' => ', $_ eq 'SCALAR' ? ${ *$glob } : $_ eq 'ARRAY' ? join ', ', @{ *$glob } : $_ eq 'HASH' ? join ', ', %{ *$glob } : # $_ eq 'CODE' ? &{ *$glob } : ## fatal as CODE slot i +s empty 'n/a' for qw[ SCALAR ARRAY HASH CODE IO FORMAT ]; print "The scalar slot is initialised to point to undef(?) '${ *$glob +}'"; 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 ]; ## HOW TO ASSIGN A VALUE TO THE CODEREF (not overwriting all the other + slots!) ## This attempts to invoke the sub # &{ *$glob } = sub{ print 'Anon 1'; }; ## This fails - $glob becomes a REF(0x....) # *{ *$glob } = sub{ print 'Anon'; }; # print "\n\nAfter \*{ *\$glob } = sub{ ... }: ", $glob; # >> After *{ *$glob } = sub{ ... }: REF(0x224fe4) ## This fails in the same way # *$glob = sub{ print 'Anon'; }; # print "\n\nAfter *\$glob = sub{ ... }: ", $glob; # >> After *$glob = sub{ ... }: REF(0x224fe4) ## And so does this. # *{ $glob } = sub{ print 'Anon'; }; # print "\n\nAfter \*{ \$glob } = sub{ ... }: ", $glob; # >> After *$glob = sub{ ... }: REF(0x224fe4) ## This (unsurprisingly) just assigns a normal coderef. # $glob = sub{ print 'Anon 2'; }; # print "\n\nAfter \$glob = sub{ ... }: ", $glob; # >> After $glob = sub{ ... }: CODE(0x18623a4) 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 ];


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 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.