| Category: | Miscellaneous |
| Author/Contact Info | Simon Proctor |
| Description: | I recently finished a large project involving a lot of OO prgramming. Tired of all the get and set methods I came up with the code below along with a co-worker in our lunch breaks. We did it more for fun than anything else so feel free to cut and paste it if you find it useful :P.
Our first attempts are commented out so you can see where we started off and what we ended up with. If there are any bugs please let me know! |
#!/usr/bin/perl
package PKG;
sub new
{
my $prototype = shift; # Allows for inheritence
my $class = ref($prototype) || $prototype;
my $self = {};
bless($self,$class);
return $self;
}
#*AUTOLOAD=sub{$AUTOLOAD=~/:(.*?)$/&&$1ne'DESTROY'&&$_[1]?$_[0]{$1}=$_
+[1]:$_[0]{$1}};
#*AUTOLOAD=sub{$AUTOLOAD=~/:(.*?)(?<!DESTROY)$/&&$_[1]?$_[0]{$1}=$_[1]
+:$_[0]{$1}};
#*AUTOLOAD=sub{$AUTOLOAD=~/:(.*?)(?<!DESTROY)$/&&($_[0]{$1}=$_[1]||$_[
+0]{$1})};
*AUTOLOAD=sub{$AUTOLOAD=~/:(.*?)(?<!DESTROY)$/&&$#_?$_[0]{$1}=$_[1]:$_
+[0]{$1}};
package MAIN;
my $object = new PKG;
$object->foo('bar');
print $object->foo(), "\n";
$object->foo(undef);
print $object->foo(), "\n";
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: Short version of autoload
by tilly (Archbishop) on Jun 03, 2001 at 04:26 UTC | |
by bikeNomad (Priest) on Jun 03, 2001 at 04:38 UTC | |
by tilly (Archbishop) on Jun 03, 2001 at 04:49 UTC | |
|
(jeffa) Re: Short version of autoload
by jeffa (Bishop) on Jun 03, 2001 at 02:36 UTC |