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:
- Restore 1.03's behavior: break with an error message
- Make it work for scalars, not just arrays and document it
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Readonly oddity
by ww (Archbishop) on Jan 26, 2016 at 13:16 UTC | |
by hippo (Bishop) on Jan 26, 2016 at 13:38 UTC | |
by Mr. Muskrat (Canon) on Jan 26, 2016 at 16:09 UTC | |
by tangent (Parson) on Jan 26, 2016 at 16:22 UTC | |
by Mr. Muskrat (Canon) on Jan 26, 2016 at 16:30 UTC | |
by mr_ron (Chaplain) on Jan 26, 2016 at 14:20 UTC | |
by choroba (Cardinal) on Jan 26, 2016 at 14:23 UTC | |
Re: Readonly oddity
by tangent (Parson) on Jan 26, 2016 at 16:28 UTC | |
Re: Readonly oddity
by mr_ron (Chaplain) on Jan 28, 2016 at 00:58 UTC | |
by SankoR (Prior) on Feb 01, 2016 at 22:43 UTC | |
Re: Readonly oddity
by dmitri (Priest) on Jan 26, 2016 at 20:58 UTC | |
by BillKSmith (Monsignor) on Jan 26, 2016 at 23:00 UTC | |
by zwon (Abbot) on Jan 26, 2016 at 23:38 UTC | |
by dmitri (Priest) on Jan 27, 2016 at 01:06 UTC | |
by kcott (Archbishop) on Jan 27, 2016 at 06:11 UTC | |
by dmitri (Priest) on Jan 27, 2016 at 15:49 UTC | |
| |
Re: Readonly oddity
by mr_ron (Chaplain) on Mar 03, 2016 at 18:02 UTC |