in reply to Re: Using "Class Data as Package Variables" to hold a global
in thread Using "Class Data as Package Variables" to hold a global

salva

Many thanks. I've had a slight snag with the syntax. If I uncomment the line in the following code I get:

Can't locate object method "set" via package "config" (perhaps you forgot to load "config"?) at myapp.pl line 9.
#!/usr/bin/perl use strict; use warnings; use Auto::Config; my $root_local = Auto::Config::config->get(root => 'local'); # This line produces the error message # config->set('root', remote => 'docroot_new'); Auto::Config::config->set('root', remote => 'docroot_new'); print "$root_local\n";

Do the methods have to be fully qualified like that? Or, more likely, have I goofed?

package Auto::Config_class; sub new { my $class = shift; my $fn = shift; my $this = { root => { local => 'c:/local', remote => 'docroot', }, }; bless $this, $class } sub get { my $this=shift; my $pri_key = shift; my $sec_key = shift; my $default = shift; exists $this->{$pri_key}{$sec_key} ? $this->{$pri_key}{$sec_key} : $default; } sub set { my $this=shift; my $pri_key = shift; my $sec_key = shift; my $value = shift; $this->{$prim_key}{$sec_key} = $value; } 1;
package Auto::Config; use strict; use warnings; require Exporter; our @EXPORT=qw(config); use lib '..'; use Auto::Config_class; my $config; sub config { $config = Auto::Config_class->new('../cwi.ini') unless defined $config; $config; } 1;

Again, many thanks.

Replies are listed 'Best First'.
Re^3: Using "Class Data as Package Variables" to hold a global
by salva (Canon) on Apr 30, 2005 at 17:59 UTC
    I had forgotten to set @ISA, check me original post, I have updated it!
      That fixed it!

      Again, many thanks.

        BTW, have you looked on CPAN for some module to handle your configuration files?

        For example, Config::IniFiles seems to be a direct fit.