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

(Russ) Re: Did I get what I expected (an array)?

by Russ (Deacon)
on Jul 16, 2002 at 16:11 UTC ( #182133=note: print w/replies, xml ) Need Help??


in reply to Did I get what I expected (an array)?

Well, in your example, print $test == scalar(@test)?, you are not testing for what you probably think you are. :-)

When you return @x into scalar context, you get the number of elements in @x. (Aside: to return the "right thing" in whatever context the user called, use wantarray) So, your test will always work, because you are doing the same thing twice.

If you don't want a reference, test against ref. If you want to prove you only returned one value (or you returned > 1), assign to an array and test with scalar, as you are.

Perhaps you should ask, "Why do I care if I have a list/array vs a scalar value?" There is no difference between your example (returning one value) and return 'test' (also one value) (except, of course, your array will not return 'test' into scalar context...).

Does it really matter, as long as you get one or more correct values?

Russ
Brainbench 'Most Valuable Professional' for Perl

  • Comment on (Russ) Re: Did I get what I expected (an array)?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-05-17 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?