Forcing Scalar Context

On occasion, you may need to force scalar context where Perl is expecting a list.  In that case, you can use the fake function scalar.  It's not a true function, because it just tells Perl to provide a scalar context:

	@rocks = qw( talc quartz jade obsidian );
	print "How many rocks do you have?\n";
	print "I have ", @rocks, " rocks!\n";	# WRONG, prints names of rocks
	print "I have ", scalar @rocks, " rocks!\n";	# Correct, gives a number

Oddly enough, there's no corresponding function to force list context.  It turns out never to be needed.  Trust us on this, too.

Next:  <STDIN> in List Context

OR

Main Menu for this topic