http://qs321.pair.com?node_id=608389


in reply to Method Chaining and Accessors

can't you just test for @_?
sub foo { my $self = shift; # oops, forgot this line before if (@_) { $self->{foo} = shift; return $self; } return $self->{foo}; }
updated to shift off self. thanks, Moron

Replies are listed 'Best First'.
Re^2: Method Chaining and Accessors
by linenoise (Acolyte) on Apr 05, 2007 at 00:09 UTC
    *slaps forehead*

    Gee, way to make a guy feel stupid! :-)

    Thanks!

Re^2: Method Chaining and Accessors
by Moron (Curate) on Apr 05, 2007 at 12:49 UTC
    Almost, but because the object arrives in $_[0] either way, you need to shift it out before testing @_, e.g. ...
    sub foo { my $self = shift; @_ or return $self -> {foo}; $self ->{foo} = shift; $self; }

    -M

    Free your mind