Re: [cgiapp] Creating SuperClass for my application
Ed, I keep a small example which takes each component thru to tested modules with documentation in pod so you see the development cycle for a base class: http://www.geocities.com/leapingfrog/perl/perl.html I hope that helps, I should create on for the newer style of plugins unless someone has already done that for the Perl presentations ... David Scott --- Ed Pigg [EMAIL PROTECTED] wrote: On Apr 24, 2006, at 12:56 PM, Michael Peters wrote: Cees Hek wrote: On 4/24/06, Ed Pigg [EMAIL PROTECTED] wrote: So how I put things like session initialization, database connection, etc.. in the SUPER::cgiapp_init sub. So my SUB CLASS would be something like sub cgiapp_init { my $self = shift; $self-SUPER::cgiapp_init(@_); $self-start_mode(); $self-run_modes( ... ); } Yes, that would work. But if you are doing stuff like session init and database connections, and you are sure you want to do this on all requests, for every runmode, then I would suggest using Michael's suggestion and set up a callback (which will always run regardless of what you do in the subclass). That is safer, since you don't have to remember to add that SUPER call in the subclass everytime. __PACKAGE__-add_callback(init = \_my_init); If you're just doing the standard stuff (session initialization, db connection, etc) there are plugins that will take care of that for you. One of the biggest benefits of those plugins is that most of them do lazy loading, so no db connection is created until you use it. It is probably a combination of too much caffine, not enough sleep, eye's bleary from looking through docs and code, but I think I made a fundamental error in my example that, when I correct it, will make things much clearer. I will keep cgiapp_init in the base class and this will handle the cgiapp_init for the sub classes. My example used cgiapp_init sub in the subclass, there by overriding the base class cgiapp_init. What I really want to do is override setup. Duh!! Thanks for all of your help and insight Michael, Cees, and Jaclyn. Ed Pigg - Web Archive: http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ http://marc.theaimsgroup.com/?l=cgiappr=1w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com - Web Archive: http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ http://marc.theaimsgroup.com/?l=cgiappr=1w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [cgiapp] Creating SuperClass for my application
On 4/24/06, Ed Pigg [EMAIL PROTECTED] wrote: Hi list, I'm trying to get the concept of creating a super class for my application and understand what elements need to be included in the base application module. Let's say I want to create a web app called MyApp. My Directory structure looks like this ... /MyApp Base.pm User.pm Inventory.pm Other.pm My Base class would be package MyApp::Base.pm use base 'CGI::Application'; sub cgiapp_init {...}; sub cgiapp_prerun {...}; sub _some_common_code {...}; 1; Then all of my other packages will contain the run modes for that module and can call methods from the base class as long as they are setup as follows package MyApp::User.pm; use base 'MyApp::Base'; sub cgiapp_init { my $self = shift; $self-start_mode( 'display_users' ); $self-tmpl_path( set_template_path_here ); $self-run_modes( qw/ display_users display_user_form edit_user /); ... module specific init items here } sub display_users {...} sub display_user_form {...} sub edit_user {...} 1; Am I understanding the layout correctly? Yes, except that in your subclass example, the fact that you create a new 'cgiapp_init' method will override the 'cgiapp_init' method in your base class. That means the 'cgiapp_init' method in your base class will not get executed. That may be desired, but if it is not, then you can call the 'cgiapp_init' method in the base class directly by adding the following line to your 'cgiapp_init' method in your subclass: $self-SUPER::cgiapp_init(@_); Cheers, Cees - Web Archive: http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ http://marc.theaimsgroup.com/?l=cgiappr=1w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [cgiapp] Creating SuperClass for my application
Cees Hek wrote: Yes, except that in your subclass example, the fact that you create a new 'cgiapp_init' method will override the 'cgiapp_init' method in your base class. That means the 'cgiapp_init' method in your base class will not get executed. That may be desired, but if it is not, then you can call the 'cgiapp_init' method in the base class directly by adding the following line to your 'cgiapp_init' method in your subclass: $self-SUPER::cgiapp_init(@_); Or you can do what I've been doing lately and just have your base class use the callback system to register it's init code. This makes it easier for your subclasses since they even have to know that the base class want's to do anything at init time. $self-add_callback(init = \_my_init); This won't work well however if you want to override your base's init in the child class. Just another way to do it :) -- Michael Peters Developer Plus Three, LP - Web Archive: http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ http://marc.theaimsgroup.com/?l=cgiappr=1w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [cgiapp] Creating SuperClass for my application
Michael wrote: Cees Heek wrote: Yes, except that in your subclass example, the fact that you create a new 'cgiapp_init' method will override the 'cgiapp_init' method in your base class. That means the 'cgiapp_init' method in your base class will not get executed. That may be desired, but if it is not, then you can call the 'cgiapp_init' method in the base class directly by adding the following line to your 'cgiapp_init' method in your subclass: $self-SUPER::cgiapp_init(@_); Or you can do what I've been doing lately and just have your base class use the callback system to register it's init code. This makes it easier for your subclasses since they even have to know that the base class want's to do anything at init time. $self-add_callback(init = \_my_init); This won't work well however if you want to override your base's init in the child class. Just another way to do it :) And for yet another way :-) It's probably not best practices, but what I do is implement the cgiapp_init, setup, etc. in the base class. Each of them makes a call to a my_init, my_setup, etc. I stub those out in the base class. Then my subclasses can implement those without overriding the base methods. If they still really need to override, that is available too. Depending on what I intend the subclasses to actually do in the my methods, I put the call at the beginning, in the middle, or at the end of the base method. Jaclyn - Web Archive: http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ http://marc.theaimsgroup.com/?l=cgiappr=1w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [cgiapp] Creating SuperClass for my application
On 4/24/06, Ed Pigg [EMAIL PROTECTED] wrote: So how I put things like session initialization, database connection, etc.. in the SUPER::cgiapp_init sub. So my SUB CLASS would be something like sub cgiapp_init { my $self = shift; $self-SUPER::cgiapp_init(@_); $self-start_mode(); $self-run_modes( ... ); } Yes, that would work. But if you are doing stuff like session init and database connections, and you are sure you want to do this on all requests, for every runmode, then I would suggest using Michael's suggestion and set up a callback (which will always run regardless of what you do in the subclass). That is safer, since you don't have to remember to add that SUPER call in the subclass everytime. __PACKAGE__-add_callback(init = \_my_init); Just put that at the top of your base class somewhere (not inside a subroutine), and make sure you are using at least CGI::Application 4.0, which is when the callback code was added. That will execute the _my_init method for every runmode request during the 'init' stage. The only problem you may run into, is that the init callbacks are called immediately after the cgiapp_init method is called. So your subclass can not do anything in cgiapp_init that depends on something that happens in your init callback, since it will not have been executed yet. Cheers, Cees - Web Archive: http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ http://marc.theaimsgroup.com/?l=cgiappr=1w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [cgiapp] Creating SuperClass for my application
Cees Hek wrote: On 4/24/06, Ed Pigg [EMAIL PROTECTED] wrote: So how I put things like session initialization, database connection, etc.. in the SUPER::cgiapp_init sub. So my SUB CLASS would be something like sub cgiapp_init { my $self = shift; $self-SUPER::cgiapp_init(@_); $self-start_mode(); $self-run_modes( ... ); } Yes, that would work. But if you are doing stuff like session init and database connections, and you are sure you want to do this on all requests, for every runmode, then I would suggest using Michael's suggestion and set up a callback (which will always run regardless of what you do in the subclass). That is safer, since you don't have to remember to add that SUPER call in the subclass everytime. __PACKAGE__-add_callback(init = \_my_init); If you're just doing the standard stuff (session initialization, db connection, etc) there are plugins that will take care of that for you. One of the biggest benefits of those plugins is that most of them do lazy loading, so no db connection is created until you use it. -- Michael Peters Developer Plus Three, LP - Web Archive: http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ http://marc.theaimsgroup.com/?l=cgiappr=1w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [cgiapp] Creating SuperClass for my application
On Apr 24, 2006, at 12:56 PM, Michael Peters wrote: Cees Hek wrote: On 4/24/06, Ed Pigg [EMAIL PROTECTED] wrote: So how I put things like session initialization, database connection, etc.. in the SUPER::cgiapp_init sub. So my SUB CLASS would be something like sub cgiapp_init { my $self = shift; $self-SUPER::cgiapp_init(@_); $self-start_mode(); $self-run_modes( ... ); } Yes, that would work. But if you are doing stuff like session init and database connections, and you are sure you want to do this on all requests, for every runmode, then I would suggest using Michael's suggestion and set up a callback (which will always run regardless of what you do in the subclass). That is safer, since you don't have to remember to add that SUPER call in the subclass everytime. __PACKAGE__-add_callback(init = \_my_init); If you're just doing the standard stuff (session initialization, db connection, etc) there are plugins that will take care of that for you. One of the biggest benefits of those plugins is that most of them do lazy loading, so no db connection is created until you use it. It is probably a combination of too much caffine, not enough sleep, eye's bleary from looking through docs and code, but I think I made a fundamental error in my example that, when I correct it, will make things much clearer. I will keep cgiapp_init in the base class and this will handle the cgiapp_init for the sub classes. My example used cgiapp_init sub in the subclass, there by overriding the base class cgiapp_init. What I really want to do is override setup. Duh!! Thanks for all of your help and insight Michael, Cees, and Jaclyn. Ed Pigg - Web Archive: http://www.mail-archive.com/cgiapp@lists.erlbaum.net/ http://marc.theaimsgroup.com/?l=cgiappr=1w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]