Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Checking number of values returned

by BrowserUk (Patriarch)
on Feb 08, 2015 at 14:39 UTC ( #1115921=note: print w/replies, xml ) Need Help??


in reply to Checking number of values returned

I'd say that unless you habitually and religiously always assign your functions to individual scalars, eg: my( $x, $y, $z ) = yourfunc();, then there is no way to do this check.

If you assign your function to:

  1. an array: my @vals =  yourFunc();
  2. or a hash: my %hash = yourFunc();
  3. or use it in a for loop: for my $x ( yourFunc() ){
  4. or to map;  ... map{ ... } yourFunc();
  5. or grep;  ... grep{ ... } yourFunc();
  6. or as an arg to any other function: someFunc( 1, 'fred', yourFunc() ); user or built-in;

then there is no way to know how many values it should return.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^2: Checking number of values returned
by jfrm (Monk) on Feb 09, 2015 at 09:39 UTC
    Well now you make me think about it, I suppose that I do habitually assign my functions to individual scalars. Perhaps this is the XY problem referred to earlier but actually I find that doing it this way makes my code much more understandable so would argue that it's a good thing. Anyway, even if I only did it only 30% of the time, it would still seems to be a valid useful thing to check for those 30% that were checkable...
      Well now you make me think about it, I suppose that I do habitually assign my functions to individual scalars. Perhaps this is the XY problem referred to earlier

      It sounds like the XY problem is that you've come to Perl from another language and haven't yet fully made the transition.

      I have the reverse problem. Every time I use C; I find my self wishing I could return multiple -- and variable numbers of -- values from functions:)

      And the funny thing is that if you always assigned your results to an array, it would be much easier to check the number of returned values:

      my @results = someFunc(); die unless @results == 3; ...

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2022-06-27 05:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (86 votes). Check out past polls.

    Notices?