I have an object that has a lot of configuration options that I set using accessors:
Then I figured out that if I return $self from each accessor I can chain my method calls:my $obj = My::Class->new(); $obj->foo("foo"); $obj->bar("bar");
This is all fine and dandy until I actually want to know what $obj->foo is set to:my $obj = My::Class->new()->foo("foo")->bar("bar");
Is there any way I can detect that I'm using the method in a chain and return the object or that I'm using it as an accessor and return the value?my $foo = $obj->foo; # Wrong! Just gets a copy of $obj.
I found this which seems to be a generic way of doing what I want but I can't decipher the magic contained in there.
I know Best Practices say that I should have separate setter and getter methods and I suspect that's the way I will have to go but having a single method that does both is just so darn elegant!
Any ideas?
In reply to Method Chaining and Accessors by linenoise
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |