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"; |
Back to
Code Catacombs