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

I am trying to use the Readonly module as recommended by Perl::Critic and it also has good reviews. However I am getting compilation errors. I have tried cutting the file down and sometimes I can get it to compile but this has not actually shed any light on the issue. Obviously I have stared at the documentation until my eyes are sore. An extract of code follows:
use Readonly; Readonly::Hash our %__DFV_DEFAULTS => { missing_optional_valid => 1, filters => 'trim', msgs => { any_errors => 'err__', prefix => 'err_', format => '<span class="dfv-errors">%s</span>', } }; sub cgiapp_init { my $self = shift; # Set some defaults for DFV unless they already exist. $self->param('dfv_defaults') || $self->param('dfv_defaults', \ %_ +_DFV_DEFAULTS); return; }
The error is Error: Global symbol "%__DFV_DEFAULTS" requires explicit package name at .....

Replies are listed 'Best First'.
Re: Hitting head against wall over Readonly compilation errors
by ikegami (Patriarch) on Sep 12, 2009 at 12:41 UTC

    Even with the missing use strict;, the code you posted should not and does not produce the error you said it does.

    >perl -c 794916.pl 794916.pl syntax OK

    And the error has nothing to do with Readonly. It means you didn't declare a variable you used.

      I only posted the bits exactly relating to the variable concerned. It is of course in a package and has "use stricts" and "use warnings". I also of course thought it should not be generating those errors which is why I am having a problem. I am trying to get a complete example but everytime I get close the problem disappears. The race is not so much who can help me spot the problem, but rather which happens first: I find the problem or I go mad.
Re: Hitting head against wall over Readonly compilation errors
by Perlbotics (Archbishop) on Sep 12, 2009 at 12:43 UTC

    Hi, I had no problem running the first part of your sample, so I guess your problem is within cgiapp_init and the error message addresses that part of your module. Since that sub is likely located in another package than %__DFV_DEFAULTS, something like \%main::__DFV_DEFAULTS might work?

      A provisional test indicates that this works (a little more to do before I break out the port to celebrate). However I am still puzzled as to what is going on here. Is the Readonly module forcing the variables into the global space? Surely that would be something terrible.
        Is the Readonly module forcing the variables into the global space? Surely that would be something terrible.

        No. perldoc -f our 'our' is not 'my'

        Is the Readonly module forcing the variables into the global space? Surely that would be something terrible.

        There is no such thing. main:: could be considered the root namespace, but it's no more or less global than any other namespaces.

        Furthermore, Readonly doesn't move or create variables. It simply alters the variable you pass to it to make it readonly. If you pass it a variable in the main:: namespace, it'll affect a variable in the main:: namespace. If you pass it a variable in the Foo::Bar:: namespace, it'll affect a variable in the Foo::Bar:: namespace. It you pass it a lexical variable, it'll affect the lexical variable.