Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

(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?

Brainbench 'Most Valuable Professional' for Perl

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

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2023-12-06 04:52 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (29 votes). Check out past polls.