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

Re: [cgiapp] Newbie: objects within CGI::Application derived class


On 5/16/05, Rhesa Rozendaal <suppressed> wrote:
> suppressed wrote:

Hi!!!

Many thanks to you, and Sam, for the replies!!!

> > This may not be strictly a CGI::Application question, but I
> > thought I'd ask here... this is my first attempt at using this
> > (and it appears *very* useful!).  I may not be grokking
> > something about perl OOP.
> >
> > I wanted to add an auth object to my application, so
> > I created something like this:
> >
> > package myApp::Auth;
> >
> > use warnings;
> > use strict;
> > use Exporter ();
> > our @ISA = qw( Exporter );
> > our @EXPORT = qw( new login );
> > our @EXPORT_OK = qw( new login );
> 
> Here's your problem: you're exporting "new" by default, so you're _replacing_ the "new" in your CgiApp derived package. That's _not_ what
> you want at all, of course. Note that _replacing_ is something completely different from _overriding_.
> Remove everything to do with Exporter. Typical OO code does not have to export anything, since your object will wrap all you need from it.

There was my "doh" moment!  Thank you, you're absolutely correct!!! 

> You should be using your own object like this:
> 
>    use myApp::Auth;
> 
>    my $auth = myApp::Auth->new( <arguments> );   # perl knows where to look for new() !
>    $auth->perform_some_method();

Yes!

> You could instantiate your object in CgiApp's setup() method, save it as a param(), and access it from there:
> 
> # in myApp::test (your CgiApp derived class)
> 
> sub setup {
>    my $self = shift;
> 
>    # store auth object
>    $self->param( auth => myApp::Auth->new( <args> ) );
> 
>    # more setup here
> }

Yes, I did something similar for a Config::IniFiles object.  But 
since I don't know much during setup, does it make more sense 
to do this during a subsequent run mode after I've acquired the 
needed credentials?

> # use it in a run mode
> sub foo {
>    my $self = shift;
> 
>    my $auth = $self->param('auth');
>    $auth->make_some_check();
> 
> }
> 
> >
> > When I use this from a simple script, everything is fine.
> > When I use it from within the CGI::App derived class (test)
> > via an instance script, I get:
> >
> > Can't local object method "_module" via package "myApp::test"
> 
> See? You exported new, but not _module. the call to new lives in myApp::test now, so it's looking for methods in the current package.
> 
> > So, some kind of namespace issue due to the fact that the
> > caller is an object (an instance of CGI::Application), it seems...
> > Any ideas about what lame-brain mistake I've made will be
> > greatly appreciated!!
> 
> Don't use Exporter unless you're exporting real functions or procedures. Read up on OO in perl (perldoc perlboot etc).

Trying to combine two different paradigms didn't help me!  ;-)

> HTH,

No question!  

Sam, your advice was perfect, but I'm perhaps too impatient.  When 
you run before you can walk, you're bound to trip, skin your nose and 
break some teeth.  I guess I have to learn the hard way!  ;-)   For this 
application, I definitely need to abstract several interfaces, including 
run modes (some can be user-supplied, handled by a built-in generic 
method, or...), authentication, and backend datastore (e.g. dB or 
LDAP).  So my feet were already moving when I hit the ground; 
just not fast enough this time!

Thanks again, Rhesa!

-r

---------------------------------------------------------------------
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.