Hello my fellow Monks,
Please forgive me for I believe I have sinned. I believe I have broken the rule of "is-a" in order to serve my own selfish purposes. I use the
CGI::Application framework for a lot of projects and one of the things I like to do is for each different section of the application I make a separate package as a separate file to keep things organized. For those who know how CGI::App is built and used there are plugin modules that take advantage of the Class (such as
CGI::Application::Plugins::DBH.) When initialized it requires that you use
$self as in
my $dbh = $self->dbh();. So in order to use
$self conveniently I abuse inheritance. For example:
package My::App;
use strict;
use warnings;
use base qw(CGI::Application BLL::OtherPackage);
sub setup {
my $self = shift;
$self->mode_param('section');
$self->start_mode('home');
$self->run_modes(
'here_sub' => 'here_sub',
'test_rm' => 'sub_in_other_file',
);
}
sub here_sub {
my $self = shift;
# no problems using self here
}
1;
### Separate file
package BLL::OtherPackage;
use strict;
use warnings;
sub sub_in_other_file {
my $self = shift;
# $self would be what I expected if I didn't put this in
# the 'use base' or @ISA for that matter. What an abuse.
}
1;
As a long time programer of Perl I feel that this is something that I shouldn't be doing and that there is a correct (and better) way of getting a subroutine's
$self to behave as if it were a method of a class even though it's in a separate file. I feel like I am doing something completely wrong. What is the best way to fix this and have methods to a class organised and separated in to separate files? I have Damian Conway's Object oriented Perl but it really hasn't given me any ideas as to how to deal with this.
My thanks for your help,