On Wednesday, September 17, 2003, at 11:38 am, Mark Fowler wrote:

On Tue, 16 Sep 2003, Steve Purkis wrote:

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 );

Firstly, I prefer proper exceptions when something goes wrong. They make
more sense than checking the return value of every returned value.

True - I tend to use die() or Error.pm myself. But the first example is misleading in that context, Tom wrote something more clear:


print "New version of foo is now " . $obj->foo( $new_foo );


That having been said, are you aware of Want? If you want to do the above
you can do something like this:


use Want;

  sub foo
  {
    my $self = shift;

return $self->_get_foo() unless @_;

    if (want('OBJECT'))
    {
       $self->_set_foo($_[0]);
       return $self
    }
    else
    {
       return $self->_set_foo($_[0]);
    }
  }

(assuming _get_foo gets the attribute, and _set_foo sets the attribute if
it can and returns false if it can't)

Cool idea, but I'd be reluctant to do something like that for performance reasons alone. I'd also like to keep the code as simple as possible -- having 2 extra method calls and another dependency is too much creep for me. Again, no reason it couldn't be done in something like 'accessors::want'...


-Steve




Reply via email to