Very nice, you just killed two birds with one stone^Wsub.
The first is the ability to cache an AUTOLOAD'ed function.
By assigning an anonymous subroutine to the typeglob
*{$AUTOLOAD}, you actually create the subroutine
'on the fly' in the package's symbol table - this means that
only the first call to this method will suffer the 'find the
right sub' performance hit. But I jumped the gun, as tilly
pointed out, your method indeed does not add the method name to
the symbol table. (thanks for providing the snippet tilly)
The second is being able to 'overload' the method so you
don't have to specify if you wish to 'get' or 'set' an
attribute. I tried to do the same thing, and failed miserably. :)
Now i see how to do it (you just create one sub that determines
which of two actions to take).
Spoilers Ahead:
The whole line starts out with a ternary operator. The
question is - do the contents of $AUTOLOAD match the regex,
and if so, is the number of arguments passed to this
'method' greater than 1?
The match catches everything except DESTROY, and as a side
effect stores the attemped method name in $1. For
example $object->foo() would be looked up as
PKG::foo, and $1 would contain 'foo'.
This match takes place on the left hand side of the &&,
and as a result, executes to see what the result is:
if true, $1 indeed contains the matched 'method' and the
right hand side of the && is evaluated.
The size of the argument list is checked via $#_,
which returns the index of the last element in the list.
Knowing that the first argument is always the name of
the object, it is clear that if $#_ is greater than zero
then the client is issuing a 'set' - if not, the client is
issuing a 'get'.
The true part of the ternary operator describes the behavior
for the 'set' function:
for example: $_[0]{$1}=$_[1]
would roughly translate to: $self->{foo} = 'bar'
if the method call was: $object->foo('bar');
And likewise, the false part defines a 'get' method:
for example: $_[0]{$1}
would roughly translate to: return $self->{foo}
if the method call was: $object->foo()
The only recommendation I would make would be to remove
the negative look-a-head for DESTROY out of the match and just
write an empty DESTROY method 'shell':
sub DESTROY {}
But that is just a personal preference.
(thanks to bikeNomad for class/object clear-up)
Jeff
R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--
|