Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-24 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found