Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Method Chaining and Accessors

by mreece (Friar)
on Apr 05, 2007 at 00:03 UTC ( #608389=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://608389]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-04-02 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?