@array1 = get_array2() || ();
The problem with this statement is that the || (logical-or) operator causes get_array2() to be invoked in scalar context. So, of course, evaluation of @array2 in the return statement of the function is in scalar context and you get the number of elements of the array as the returned value.
There may be a conceptual disjunction here: while a scalar can be undefined or hold an empty string (or a few other things), an array only ever holds zero or more scalar elements. There is no such thing as an "undefined" array. (Update: Of course, an array element, if any exist, is always a scalar, so you could have an array with one or more undefined elements.)
Update: Take a look at Context tutorial in the PerlMonks Tutorials.
Update 2: Another conceptual problem with the quoted statement is rooted in the fact that a function only returns a list of zero or more elements. So what the statement is saying (or trying to say) is "assign the return list of the function call to the array unless the list is empty, in which case assign the literal empty list," which kinda doesn't make a lot of sense.
Update 3: Changed "zero or more elements" to "zero or more scalar elements" in second paragraph above just for sake of clarity.
Give a man a fish: <%-{-{-{-<
In reply to Re: Return array from sub or return empty array
by AnomalousMonk
in thread Return array from sub or return empty array
by gregzartman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |