Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: CGI::Application and object inheritance

by cfreak (Chaplain)
on Jul 27, 2005 at 14:39 UTC ( [id://478617]=note: print w/replies, xml ) Need Help??


in reply to CGI::Application and object inheritance

I'm not all that familiar with CGI::Application but I don't think you're doing the subclassing right. You shouldn't need @ISA at all. use base 'CGI::Application' should handle that for you. It doesn't look like you ever call the parent constructor. Here's how I would do it:
package Foo; use base qw(CGI::Application); use Bar; sub new { my $class = shift; # SUPER refers to CGI::App because of 'use base' my $self = $class->SUPER::new(); $self->{ stuff } = 'whatever'; $self->{ bar } = Bar->new(); # I wouldn't use 'super' as a key he +re return $self; } # etc ...

Now in your script:

use Foo; my $app = Foo->new();

... should have access to all of Foo's methods and all of CGI::App's methods

Replies are listed 'Best First'.
Re^2: CGI::Application and object inheritance
by spork (Monk) on Jul 27, 2005 at 15:45 UTC
    Thanks for the input cfreak. What is missing from the code sample is the actual CGI script which calls the new() defined in C::A. I am new to OO use and your answer sheds some light on where my package fits in the big picture. Thanks again.

      Ahh but it does

      In your script you just need to call:

      my $cgi_app = Foo->new();

      Sorry that wasn't clearer. new() in package Foo makes the call my $self = $class->SUPER::new(). Because of the use base statement SUPER now refers to C::A. $cgi_app is an object from which you can access your own methods or the C::A methods. If you need to pass opts to C::A you can modify new() in Foo. I'd modify it so you can pass the opts to the Foo package and it passes them on.

      Hope that makes it a bit clearer

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://478617]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found