in reply to Can't understand function returning undefs

You're expecting a true/false value from your function a(). However, the expression in your if statement isn't a(), it's a list assignment. And, in scalar context, list assignment returns the number of elements on the RHS of the assignment. So, if a() returns 2 elements, the assignment returns 2 (changing the variables on the LHS is a mere side-effect of the assignment). It's only the quantity of the elements returned by a() that matters, not their value.

Replies are listed 'Best First'.
Re^2: Can't understand function returning undefs
by rg0now (Chaplain) on Sep 24, 2010 at 21:50 UTC
    Here is what drives me crazy:
    if(undef, undef){ print "OK1\n"; } if( (undef, undef) ){ print "OK2\n"; } my @a = (undef, undef); if(@a){ print "OK3\n"; }
    This prints OK3. According to the above, it should also print OK2 because (undef, undef) is a list of two elements that evaluates to 2 in scalar context. Still, it doesn't.
      There are no lists in scalar context! It's SCALAR context. What you have in
      if (undef, undef) { # An extra set of parens doesn't matter. print "OK1\n"; }
      is the comma operator in scalar context. Which evaluates its LHS, throws it away, then returns its RHS. Which is undef, and hence false.
      According to the above
      If with "above" you mean the post you're replying to, you notice I talk about list ASSIGNMENT. Not lists. Nor arrays.

        is the comma operator in scalar context.

        It's funny how you say something doesn't exist right before you say the thing that doesn't exist is what he has. (Sure, you used a different name for it.)