Using Scalar-Producing Expressions in List Context

Going this direction is straightforward:  if an expression doesn't normally have a list value, the scalar value is automatically promoted to make a one-element list:

	@fred = 6 * 7;	# gets the one-element list (42)
	@barney = "hello" . ' ' . "world";

Well, there's one possible catch:

	@wilma = undef;	# OOPS!  Gets the one-element list (undef)
			# which is not the same as this:
	@betty = ();	# A correct way to empty an array

Since undef is a scalar value, assigning undef to an array doesn't clear the array.  The better way to do that is to assign an empty list 1

Next:  Forcing Scalar Context

OR

Main Menu for this topic




























[1] Well, in most real-world algorithms, if the variable is declared in the proper scope, it will never need to be explicitly emptied.  So this type of assignment is rare in well-written Perl programs.  We'll learn about scoping in the next lesson.

Return to the page from whence you came