One thing that is quintisential to all object orriented programming is inheritance. I don't believe your script allows for that (what if module script use()es it, and put @ISA = yourModule). I'm sure there are other ways to preserve inheritance, but the one I've used is that of CGI.pm, which has a self_or_default function. That function creates a blessed object, if there is not one to begin with. A more simple version, which would be fine for you would be:
use strict;
my $Q;
sub return_with_object {
unless (defined($_[0]) &&
(ref($_[0]) eq 'ModuleNAME' || UNIVERSAL::isa($_[0],'ModuleNAME'
+))
)
{
$Q = ModuleNAME->new unless $Q;
unshift(@_,$Q);
}
return wantarray ? @_ : $Q;
}
All the subroutine does is checks that an object is defined, and that it is the object for the class you are using. If there is no object, the script creates one, for use throughout the script, without the user knowing that it exists. It's actually a really cool idea, because an oblivious user can't mess with an object, although it exists, but I'm sure some guru has a reason not to use it. It also preserves inheritance by using Universal::isa (
UNIVERSAL::isa ( VAL, TYPE ) ), which returns true if the VAL is the name of a package that inherits from (or is itself) package TYPE.
Update: And to use the subroutine -
sub SomeSubWithNothingElseToShift {
my $self = return_with_object(@_);
}
sub SomeSubWithOtherStufftoShift {
@_ = return_with_object(@_);
my $self = shift;
my $barz = shift;
}
Gyan Kapur
gyan.kapur@rhhllp.com
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.