in reply to undef var

The first is being evaluated in a list context, because print takes a list, but the second is evaluted in a scalar context because you're concatenating this. In a scalar context, a list evaluates to the number of elements it contains.

If you change the contexts in your examples, you'll get the opposite results:

perl -e 'print scalar(my ($rec) = undef);' perl -e 'print "return=",(my ($rec) = undef);'
The first explicitly asks for a scalar context, and prints 1. The second stays in print's list context and so prints nothing (well, just return=).

Replies are listed 'Best First'.
Re^2: undef var ("list in scalar context")
by tye (Sage) on Jul 03, 2003 at 21:03 UTC
    In a scalar context, a list evaluates to the number of elements it contains.

    More precisely, in a scalar context a list assignment returns the number of items on the right-hand side.

    If anyone ever says of Perl "a list in a scalar context", then you know they've fallen victim to a very common misconception. They'll usually end that with "returns the last item in the list", but that wouldn't have made sense in this case.

    The fact is that there is no one rule for what you get when you "put a list in a scalar context". Some would even say that you can't put a list into a scalar context. I prefer to say that it depends on what the "list" is. Because I don't feel that "list" is a well-defined term in Perl.

    There are a great many "things that would return a list if used in a list context" and each of them can have their own rule for what they return if used in a scalar context. The most common is "last item", though exactly what an "item" is can be a bit tricky to define. (:

                    - tye