in reply to need some OO help

Just put an empty ("abstract") method in your base class, which your subclasses will use to get the path.

package BaseConf; my $Conf; sub _load_config { my $self = shift; my $conf_file = $self->config_file_path; croak "unable to find $conf_file" unless -e $conf_file and -r $conf_file; $Conf = Config::Tiny->read( $conf_file ); croak "read config file '$conf_file' failed: $Config::Tiny::errstr" unless $Conf; } sub config_file_path { croak "config_file_path must be provided by subclass."; } ... package MyConf; use base 'BaseConf'; sub config_file_path { return '/path/to/conf.file'; }

Replies are listed 'Best First'.
Re^2: need some OO help
by Qiang (Friar) on Mar 26, 2007 at 19:59 UTC
    thanks. that should fix it except..

    since i am using BEGIN { _load_config() } in base class. how do i get around using the method you suggested?

      Put it in your derived class instead (and turn it into a class method.)

      package MyConf; ... BEGIN { __PACKAGE__->_load_config; }

      That will call _load_config (as a method) in your parent class, which will call config_file_path in your child class.

        i see. in that case, i could just have the following line in my subclass:
        BEGIN { __PACKAGE__->load_config('path/to/my.conf') }
        i prefer to not knowing that kind of detail in the parent class. another way that may look cleaner is
        package BaseConf; sub config_file_path { croak "must implemeted in subclass" } sub load_config { my $self = shift; my $conf_file = $self->config_file_path; croak "unable to find $conf_file" unless -e $conf_file and -r $con +f_file; my $cfg = Config::Tiny->read( $conf_file ); croak "read config file '$conf_file' failed: $Config::Tiny::errstr +" unless $cfg; return $cfg; } ub get { my $self = shift; my $cfg = $self->load_config; croak "invalid method called $_[0]" if not exists $cfg->{_}->{ $_[ +0] }; return $cfg->{_}->{ $_[0] }; } sub import { ... } sub AUTOLOAD { .. } package MyConf; use base 'BaseConf'; sub config_file_path { return "path/to/my.conf" }
        in plain CGI, Config::Tiny object is created for every config directive in sub import {}. maybe bad for performance. i am not sure if the same C::T object gets shared under mod_perl env.