http://qs1969.pair.com?node_id=1153664

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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: