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,
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.