Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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.

-stvn


Comment on Re^3: Moose Design Thoughts (Traits)
Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2014-07-24 18:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (165 votes), past polls