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

I ran into this problem while trying to configure RDBO connections on startup by reading a config file. RDBO has a nasty habit of doing a lot of things during compile time, so the essence boils down to:

#!/usr/bin/perl BEGIN { use Config::Std; read_config "main.conf" => my %config; }

...which runs fine (assuming, you've got a valid main.conf), but warns:

Name "Config::Std::Hash::DEMOLISH" used only once: possible typo at /usr/share/perl5/Class/Std.pm line 523.

No problems on execution. It only happens when %config is a lexical though, which I suspect to be part of the problem.

I searched for this, but other than a similar but unrelated warning thrown by LedgerSMB this does not seem to happen to other people. I could just try to make it go away, but I'd rather understand the cause before that.

Any ideas?

Edit: I'll need to clarify.

The main code is nowhere near this snippet. In reality the config is encapsulated into a Moose object that reads the config in its BUILD into an attribute, and will then be instanciated elsewhere, and a long way down the line will be handed to RDBO.

The actual code runs under strict, and the snippet is only for quick reproduction. If you need it even shorter, try:

perl -le 'BEGIN { use Config::Std; read_config "main.conf" => my %config }'

Feel free to add strictures and warnings to your liking, it should not change the outcome. What does change the outcome though is a different perl version. 5.8.8 does not give the warning, 5.10.0 and 5.11.0 do. *sigh*

Replies are listed 'Best First'.
Re: Config::Std to lexial in BEGIN { } throws warning?
by MidLifeXis (Monsignor) on Feb 23, 2010 at 17:06 UTC

    I believe that %config goes out of scope at the end of the BEGIN block. When the BEGIN block completes, DEMOLISH is able to be called, because %config has just gone out of scope. Now, whenever you are using %config in the rest of your application, I am not certain where the data is coming from.

    If you move my %config outside of the BEGIN block, and then change the read_config line to read_config "main.conf" => %config, it should take care if the problems, and you will actually have the data from your configuration file used by the application.

    This would have been told you if you had done a use strict; use warnings; before the BEGIN block.

    It is said that "only perl can parse Perl." I don't even come close until my 3rd cup of coffee. --MidLifeXis

      Updated origial post to reflect MidLifeXis post.

      Unfortunately, I cannot move the my outside, because the code never gets a chance on that with RDBO. Just useing a model runs most of it immediately.

        So this does not do what you want it to?

        use Config::Std; my %config; BEGIN { read_config "main.conf" => %config; }

        It is said that "only perl can parse Perl." I don't even come close until my 3rd cup of coffee. --MidLifeXis