Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^4: most appropriate return data type

by merlyn (Sage)
on Feb 19, 2010 at 16:43 UTC ( #824200=note: print w/replies, xml ) Need Help??

in reply to Re^3: most appropriate return data type
in thread most appropriate return data type

  • the qw// operator is the comma operator in disguise
It is now. It used to be the split function, in which case, you'd get a split-in-a-scalar-context which would clobber @_ and trigger a warning and return 4. :)

-- Randal L. Schwartz, Perl hacker

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Replies are listed 'Best First'.
Re^5: most appropriate return data type
by TGI (Parson) on Feb 24, 2010 at 20:38 UTC

    I didn't know that about the changes to qw//.

    I've had some rude surprises when I've written subs that were intended to return lists. That's why I always use an intermediate variable or an explicit wantarray call in when I write a sub that will normally return a list. Things like return 7..5 can have surprising effects in scalar context.

    Most of the time, a sub that returns a list should act like an array in scalar context, assigning to an intermediate array is a simple way to get context aware results. When other behavior is desired, I prefer to explicitly construct the result set using wantarray.

    sub seven_in_scalar_context { my @r = 3..9; return @r; } sub nine_in_scalar_context { my @r = 3..9; return wantarray ? @r : $r[-1]; }

    Array behavior in scalar context is pretty well known, so I feel good about using it implicitly. I've seen too many questions about other kinds of contextual returns (especially the regarding the comma) to feel good about using them implicitly. So I use wantarray everywhere else.

    TGI says moo

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://824200]
[Eily]: yup, he's part of a secret agent team or something like that
[Your Mother]: Haven't seen it, but let me guess. There is a woman and a minority agent and no Germans? Close? :P
[Eily]: you're good :P
[Eily]: except if you count their boss, which is both a woman and from a minority
[LanX]: na, they go easy on germans since they discovered the German TV market
[Your Mother]: Zeitgeist. :P
[LanX]: Talking about Peacemakers, I still need to watch 2 seasons of Farscape ...
[Your Mother]: I liked that show a lot.
[Your Mother]: Packed with minority women.
[LanX]: pitty they burned the actors in Stargate

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-03-19 14:39 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.