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

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


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.

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.

 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.

    http://search.cpan.org/~ceeshek/CGI-Application-Plugin-Authorization-0.03/lib/CGI/Application/Plugin/Authorization.pm#cgiapp

    Mark

-- 
http://mark.stosberg.com/ 


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


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