DEV Community

Discussion on: 5 Ways You Can Embrace the Magic of Perl

Collapse
 
grinnz profile image
Dan Book

But in the second line, we’re assigning to the scalar variable $bar, which now contains the number of items in the list.

This isn't correct in this example; in scalar context, the comma evaluates the expression to the left and to the right of it and returns the expression to the right. So it happens to return 3 in this case because that's the last expression. Returning the size in scalar context is behavior specific to array variables and functions like grep (but many functions do something different, as you note).

Collapse
 
mjgardner profile image
Mark Gardner

Thanks! I’ve made the correction.

Collapse
 
choroba profile image
E. Choroba

True. I usually use strings or numbers like 101, 102 in examples and tests to not fall into this trap.