Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"used only once" warnings with singleton classes #45

Closed
ajolma opened this Issue Sep 21, 2018 · 3 comments

Comments

Projects
None yet
1 participant
@ajolma
Copy link
Contributor

ajolma commented Sep 21, 2018

I'm trying to create a singleton class which does its initialization in BEGIN block, i.e.,

package test;
my $instance;
sub get_instance {
    my $class = shift;
    return $instance if $instance;
    my $pc = PkgConfig->find('dependency);
   # do stuff with $pc ...
    $instance = {};
    return bless $instance, $class;
}
BEGIN {
    require PkgConfig;   
    $instance = test->get_instance();
}
1;

Using this in main program is simple but the instance is available through get_instance().

However, if the dependency package's pc-file does not contain en entry for 'url' for example, a warning is issued

Name "PkgConfig::Vars::SERIAL_1::url" used only once: possible typo at PkgConfig.pm line 523.

This warning does not appear if the first call to get_instance is from the main program. Furthermore, I haven't found a way to suppress the warnings.

@ajolma

This comment has been minimized.

Copy link
Contributor Author

ajolma commented Oct 2, 2018

no warnings qw(once);
Before require PkgConfig; seems to do the trick.

@ajolma

This comment has been minimized.

Copy link
Contributor Author

ajolma commented Oct 2, 2018

no, no warnings qw(once); must be in sub _pc_var in PkgConfig.pm

@ajolma

This comment has been minimized.

Copy link
Contributor Author

ajolma commented Oct 3, 2018

Fixed by b67a8c8. Closing.

@ajolma ajolma closed this Oct 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.