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

Hi, Monks. We have a problem.
We have a 2 package & 2 object & 1 script.

test.cgi
use strict; use ConfigFile; use TestClass; my $conf = new ConfigFile; my $object = new TestClass; $object->test();
ConfigFile.pm
package ConfigFile; sub new { my $that=shift; my $class=ref($that) || $that; my $self={ Verbose => 1, RaiseErrors => 0}; bless $self, $class; return $self; } sub get_conf { my $self=shift; my $param=shift; my $result; # get result return $result; } 1;
TestClass.pm
package TestClass; sub new { my $that=shift; my $class=ref($that) || $that; my $self={ Verbose => 1, RaiseErrors => 0}; bless $self, $class; return $self; } sub anything { my $self=shift; # # Here we nave a get object $conf from a test.cgi script. # return; } 1;
Problems:
We need a use object $conf in method anything from object TestClass. How we can do this?

we know a 2 variants:
1. $object = new TestClass (conf_obj => $conf);
2. $object->anything($conf);

But this structure don't make me happy. I want a something also.
SUPER ? ISA ? Please Help me.
P.S."I'm sorry for my English"

Replies are listed 'Best First'.
Re: Help with objects.
by izut (Chaplain) on Mar 14, 2006 at 13:10 UTC

    I think you're getting confused about OO patterns. You must ask yourself: will TestClass be a ConfClass or it will have a ConfClass? After you answered that question, you will be able to implement it. If TestClass will BE a ConfClass:

    package TestClass; use base qw(ConfClass); sub new { my $class = shift; my $self = $class->SUPER::new(); # Initialize the superclass ConfC +lass here. ... # Do whatever you want. return $self; }

    If TestClass HAS a ConfClass object, then it should be someway like this:

    package TestClass; use ConfClass; sub new { my $self = {}; bless $self; $self->conf(ConfClass->new()); } sub conf { my ($self, $conf) = @_; $self->{'_CONF'} = $conf if $conf; return $self->{'_CONF'}; }

    Igor 'izut' Sutton
    your code, your rules.

Re: Help with objects.
by xdg (Monsignor) on Mar 14, 2006 at 14:08 UTC

    In addition to other suggestions above, if your configuration is going to always be the same for a given script, you could consider making it a singleton object. Then the constructor for TestClass could create a private configuration object without it having to be explicitly passed in.

    package ConfigFile; my $self; sub new { if ( ! defined $self ) { my $that=shift; my $class=ref($that) || $that; $self = { Verbose => 1, RaiseErrors => 0}; bless $self, $class; } return $self; } sub get_conf { my $self=shift; my $param=shift; my $result; # get result return $result; } 1;
    package TestClass; require ConfigFile; sub new { my $that=shift; my $class=ref($that) || $that; my $self={ Verbose => 1, RaiseErrors => 0, Config => ConfigFile->new() }; bless $self, $class; return $self; } sub anything { my $self=shift; # Access config via $self->{Config}->get_conf() return; } 1;

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Help with objects.
by pajout (Curate) on Mar 14, 2006 at 10:36 UTC
    I am not really sure, but, do you want to do something similar to this?
    package TestClass; use ConfigFile (); #... sub anything { #... my $conf = ConfigFile->new(); #... }
      It's a very siple variant. But we look for something - sub anything { $conf=SUPER::conf; }
        My English is poor, so I am not able to understand, what do you really want...
        You have two modules, both implements method 'new'. So you can create instance of the class everywhere in your code, with no care about ISA or SUPER...
        Is your problem that the $conf does not match your criteria and you would rather used the class-parent of the $conf instance?