Though not a recommended practice (in theory), one could put both OO and non-OO codes in a package. In that case, you would use Exporter for the non-OO code. Take the following as an example
There are a _DB.pm...
# Constructor
# Usage: $MyDB = DB::MyDB->dbconnect("db", "username", "pwd")
sub dbconnect {
# stuff
}
# Usage: $tbl = $MyDB->selectall_tbl($select_statement)
# where $tbl is a 2D array
sub selectall_tbl {
# stuff
}
1 ;
.. and a MyDB.pm...
package DB::MyDB ;
require Exporter ;
@ISA = qw(Exporter) ;
@EXPORT = qw(print_tbl) ;
use DBI ;
use strict ;
use DB::_DB ; # OO stuff
# Usage: print_tbl($tbl)
# where $tbl = $MyDB->selectall_tbl($select_statement)
# return nice text result
sub print_tbl {
# stuff
}
1 ;
... and a MyWebDB.pm
package DB::MyWebDB ;
use Apache::Reload ;
use strict ;
use DB::_DB ; # OO stuff
1 ;
_DB.pm contains all the OO stuff for DB access--which basically forms a base object. But there are an Apache environment and a regular environment, where you might need additional methods/subroutines specific for them respectively. Well, you could write some child objects, which inherit the base object from _DB.pm, with additional methods.
Or, more lazily, you could just write some subroutines and export them. Since subroutines (or closure, for that matter) are theoretically less CPU- and memory-expensive than objects and methods, and in Perl, less code to write, it might not be all that unreasonable a thing to do in Perl--which, as a good thing, doesn't force you to program one way or the other. Just as in Java when you need to write an utility class, which contains no data and nothing but class methods, in Perl you just write a "package."
In other words, the question one might ask is: are these really methods, or could these just be subroutines or closure?
(O, yah, you could merge MyDB.pm and MyWebDB.pm together by dynamically determining where you are, as suggested in a previous reply, in case you want to get cute.)
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.