I don't understand. Why would $guid{218827} be auto-vivified? Note that $guid{218827} is referenced from a subroutine, which isn't executed until after the BEGIN block.

I think there's a bug in Perl. Watch what happens if you dump the content of %guid in the BEGIN block:

#!/usr/bin/perl use warnings; my %guid=(218827=>5,109940=>2); BEGIN { %handle= ( cmd => sub { my $ref = sub{$guid{218827}++;warn "sub: $guid{218827}";}; $ref->(); }, ); while (my ($key, $value) = each %guid) { print defined $key ? $key : "UNDEF"; print ":"; print defined $value ? $value : "UNDEF"; print "\n"; } } $handle{cmd}->(); __END__ sub: 6 at buu line 14.
Not only does it show that the BEGIN block doesn't autovivify the value, the error disappears as well!

In fact, just about any reference to %guid (like keys %guid;, %guid;, or $guid {1};) after the assignment to %handle makes the error go away.

This ought to be perlbugged.

Abigail


In reply to Re: modification of read-only in hash ++ by Abigail-II
in thread modification of read-only in hash ++ by BUU

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.