Ed Pigg wrote:
I've split my application run modes into several modules and a base
class.
myapp.pm
somestuff.pm
someotherstuff.pm
I want to put the common code in the base class. Session management -
CGI::Application::Plugin::Session, Authentication -
CGI::Application::Plugin::Authentication, etc..
I'm having a problem getting the derived modules to call base class
methods. Do I need to export the methods?
No. Inheritance should make sure your subclasses can call methods that are
defined in the base class.
package myapp::Base;
use strict;
use warnings;
use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::Authentication;
use base 'CGI::Application';
sub common_function {
}
So far this looks good.
package myapp::somestuff;
use strict;
use warnings;
use base 'myapp::Base';
my somestuff_function {
my $self = shift;
my $value = $self->common_function(); -----> error
common_function not found
}
This ought to work. There must be another reason it errors for you.
Maybe you should show us some more code. I don't think we can tell why it
doesn't based on what you've given us sofar.
Shouldn't all of the base class functions be inherited?
Yes. That's the whole idea of Object Oriented programming. Here's a full
example, written as a test. The base class integrates CAP::Redirect, and the
sub class actually calls the method from that plugin.
{
package My::BaseApp;
use CGI::Application::Plugin::Redirect;
use base 'CGI::Application';
package My::SubApp;
use base 'My::BaseApp';
sub setup {
my $self = shift;
$self->start_mode('screen');
$self->run_modes([ qw/ screen / ]);
}
sub screen {
my $self = shift;
return $self->redirect('/foo/bar');
}
}
$ENV{CGI_APP_RETURN_ONLY} = 1;
my $sub_app = My::SubApp->new; # instantiate new object
my $output = $sub_app->run; # and run the app
use Test::More tests => 4;
isa_ok( $sub_app, 'My::BaseApp');
isa_ok( $sub_app, 'CGI::Application');
can_ok( $sub_app, 'redirect');
like ( $output, qr(Location: /foo/bar), 'subclass redirects properly');
__END__
1..4
ok 1 - The object isa My::BaseApp
ok 2 - The object isa CGI::Application
ok 3 - My::SubApp->can('redirect')
ok 4 - subclass redirects properly
Rhesa
--
#!/usr/bin/perl
tie %rope, 'Tree' && hang $self;
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[email protected]/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]