Consider the following script:

#!/usr/bin/perl use strict; use warnings; use Readonly; use feature qw/say/; Readonly my $foo = 'bar'; Readonly my @baz = (qw/glug argh/); say "foo is '$foo' and baz is '@baz' in main"; say "Perl version is $] and Readonly version is $Readonly::VERSION";

On this system, it generates the following output:

Use of uninitialized value $foo in concatenation (.) or string at read +only_fail.pl line 11. foo is '' and baz is 'glug argh' in main Perl version is 5.020003 and Readonly version is 2

You might have noticed that the script above is subtly broken. I should have written Readonly my $foo => 'bar' . But the mistake is easy to make, as it is very natural to write my $foo = 'bar' and it is plausible to think that "Readonly" works by merely extending this syntax.

The insidious thing is that on this system, with these versions of Perl and Readonly it doesn't complain at all, it just silently leaves the variable undef. Even worse, it appears to work with arrays!

For that matter, the very same script breaks on my other system with Perl 5.14 and Readonly 1.03.

Is this a known problem?

If not, I see two approaches to fix it:


In reply to Readonly oddity by Anonymous Monk

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.