in reply to How to morph a plain module to OO
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.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; }
sub SomeSubWithNothingElseToShift { my $self = return_with_object(@_); } sub SomeSubWithOtherStufftoShift { @_ = return_with_object(@_); my $self = shift; my $barz = shift; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to morph a plain module to OO
by rir (Vicar) on Sep 08, 2002 at 18:18 UTC |