> I apologize in advance for asking a tenuously related CGI::App question.
>
> My CA app extends my own framework class which creates a Configuration
> object and has methods to get to it, then it calls actions which can get
> to the configuration. Fine.
>
> But I have created another object "SearchEngine". When I instantiate
> that in my action... it knows nothing of the Configuration
> object/methods.

Actually, it's a very good question.  All of the various CGI::App config
plugins put a config method into your $self object.  But how do you get
at this data outside of your CGI::App object hierarchy?

A typical example would be configuring your Class::DBI classes from
within CGI::App.  Your Class::DBI base class is expected to provide a
db_Main method returning a database handle.  The problem is, this method
knows nothing about your CGI::App object:

    package MyCDBI;
    sub db_Main {

        # get configuration from somewhere
        my $conf = ???

        my $dsn  = $conf->{'connect_string'};
        my $user = $conf->{'username'};
        my $pass = $conf->{'password'};

        return DBI->connect_cached($dsn, $user, $pass);
    }

One way of approaching this is to store the configuration as class data
as soon as you initialize it:

    package MyApp;
    sub cgiapp_init {
        my $self = shift;

        my $config = get_config_somehow();

        # store the configuration as class data
        $MyProject::Current_Config = $config;

    }


Then your unrelated class can fetch this data:

    package MyCDBI;
    sub db_Main {

        # get configuration from somewhere
        my $conf = $MyProject::Current_Config
            or die "System not configured yet!"

        my $dsn  = $conf->{'connect_string'};
        my $user = $conf->{'username'};
        my $pass = $conf->{'password'};

        return DBI->connect_cached($dsn, $user, $pass);
    }


I don't know about the other CGI::Application plugins, but
C::A::P::Config::Context does this for you already.  It provides a class
method for getting at configuration data:

    my $config = CGI::Application::Plugin::Config::Context->get_current_context;


Michael



--
Michael Graham <[EMAIL PROTECTED]>


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[email protected]/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to