Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^3: Moose Design Thoughts (Traits)

by stvn (Monsignor)
on Dec 23, 2010 at 19:21 UTC ( #878856=note: print w/replies, xml ) Need Help??

in reply to Re^2: Moose Design Thoughts (Traits)
in thread Moose Design Thoughts (Traits)

It is pretty much no longer recommended anywhere because it (ab)uses context and returns array/hash ref in scalar context and an array/hash in list context. While this is actually a fairly common idiom in Perl which I myself am long guilty of (ab)using (heck I even wrote the original auto-def code), it really is evil and should be stopped. The problem is that Perl context sensitivity can easily be the source of annoying and difficult to spot bugs, like for instance ...

# normal usage, works just fine ... my @foo = $obj->foo; my $foo = $obj->foo; # but ... $other_obj->bar( foos => $obj->foo ); # whoops! # should actually be the more ugly ... $other_obj->bar( foos => scalar $obj->foo );
While it might seem trivial, it really can be difficult to track down sometimes, especially when this is your primary accessor. But of course, it is nice to not have to manually de-ref all the time (Perl 5.13++) which is pretty much why the Hash trait and Array trait both have an 'elements' to delegate to that returns the derefed item in a non-context sensitive way.


Replies are listed 'Best First'.
Re^4: Moose Design Thoughts (Traits)
by Cagao (Monk) on Dec 24, 2010 at 11:57 UTC

    Thankfully I've actually started to move to drop 'wantarray' at the end of my subs for those very reasons.

    I think I'm much happier for a list to always be returned as a list, rather than, as you say, bizarre behaviour at times.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://878856]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-06-21 10:54 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.