[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Re: Help with referencing cgiapp->dbh inside a plugin


Mark Stosberg wrote:
On 2006-03-16, Paul Johnston <suppressed> wrote:
Right - I have a plugin that is essentially very simple - we'll use the blog example from previous thread.

I have created it with one Exported method (for example blog) which stores the object in the calling objects private hash ie:

--- snip ---
package Plugins::Blog;
use vars qw($VERSION @EXPORT @ISA);
use strict;

require Exporter;

$VERSION = 0.01;
@ISA = qw(Exporter);
@EXPORT = qw(blog);

sub blog {
    my $app = shift;

    if(!$app->{__PACKAGE__ . '__oBlog'}) {
       # create the object for this module and store it in the app
        my $blog = {};
        bless $blog;
        $app->{__PACKAGE__ . '__oBlog'} = $blog;
    }

    return $app->{__PACKAGE__ . '__oBlog'};
}
--- snip ---

Now - the question I have is (assuming cgiapp is the calling object) how do I in a different method retrieve the database handle? ie

--- snip ---
sub getBlog {
    # this is the object itself, not the cgiapp
    $self = shift;
    $dbh = $self->dbh; # ??? confused!
}
--- snip ---

Help would be much appreciated. I've tried this various ways (putting a reference to the app's dbh method in the object), but can't figure it out. I'm going wrong somewhere and have looked at other code, but can't quite figure it out.

Paul,

You are creating a new blog object, but you aren't storing anything in
it or giving it any methods.

Missed the point of the post as the object is a hypothetical object - the post was to figure out how to store a reference to the cgiapp or dbh object more than anything else.

Figured out that you need to store a reference to the cgiapp or dbh object after the line "bless $blog;" and then the reference is stored. :)

Two approaches come to mind:

 1. Split your work into a standard OO module and a simple wrapper
    plugin. See HTML::Prototype and the HTMLPrototype plugin for an
    example of this technique. This is the approach I recommend. It
    means your work will be useful outside of CGI::Application, possibly
    gaining you other users and contributors.

:) Thanks - Haven't seen HTML::Prototype - will take a look.

2. Give your blog object a dbh() method, or a cgiapp() method which provides access to the CGI::Application object. See the new()
    and cgiapp() methods of the Authorization plugin for an example
    implementation.

My post was actually asking how to do this simply. I've taken a look at the Authorization plugin and figured it out from that and other plugins.

Thanks

Paul

Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.