On Tue, 16 Sep 2003, Steve Purkis wrote:
Hi all,
With the help of others, I've been bashing out an 'accessors' pragma[1]:
use accessors qw( foo bar baz );
There are two popular styles of accessor I'd like to support:
# classic: print "I set foo!" if $obj->foo( $a_value );
# chaining: $obj->foo( $a_value ) ->bar( $another_value );
This whole chained accessors thing is a definite perl-ism and will do you
no favours with people for whom Perl is not a primary language.
AFAIK, it was stolen from Smalltalk...
Possible return values for write-accessors:
true/false undef/error message the previous value the new value [I would never have considered the object itself a valid return value until I saw Perl people doing it. I still don't.]
Question is: what style should be the default? I'm not looking for a debate here, just some numbers. If you don't wanna post to the list, reply to me directly.
I tend to use a simple true/false, except in complex cases where I return
undef or an error message. There's no point putting any effort into giving
the programmer something he already has. All of the other options fall
into this category [object, new value, previous value].
I see your point, but I also think it's very much up to the individual.. which is why I tread carefully to avoid a style war ;)
Thanks for the feedback, -Steve