Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the scalar and list contexts in Perl?

Tags:

list

perl

scalar

What is the difference between the scalar and list contexts in Perl and does this have any parallel in other languages such as Java or Javascript?

like image 229
user47145 Avatar asked Dec 18 '08 04:12

user47145


2 Answers

Various operators in Perl are context sensitive and produce different results in list and scalar context.

For example:

my(@array) = (1, 2, 4, 8, 16);
my($first) = @array;
my(@copy1) = @array;
my @copy2  = @array;
my $count  = @array;

print "array: @array\n";
print "first: $first\n";
print "copy1: @copy1\n";
print "copy2: @copy2\n";
print "count: $count\n";

Output:

array: 1 2 4 8 16
first: 1
copy1: 1 2 4 8 16
copy2: 1 2 4 8 16
count: 5

Now:

  • $first contains 1 (the first element of the array), because the parentheses in the my($first) provide an array context, but there's only space for one value in $first.
  • both @copy1 and @copy2 contain a copy of @array,
  • and $count contains 5 because it is a scalar context, and @array evaluates to the number of elements in the array in a scalar context.

More elaborate examples could be constructed too (the results are an exercise for the reader):

my($item1, $item2, @rest) = @array;
my(@copy3, @copy4) = @array, @array;

There is no direct parallel to list and scalar context in other languages that I know of.

like image 171
Jonathan Leffler Avatar answered Nov 17 '22 21:11

Jonathan Leffler


Scalar context is what you get when you're looking for a single value. List context is what you get when you're looking for multiple values. One of the most common places to see the distinction is when working with arrays:

@x = @array;  # copy an array
$x = @array;  # get the number of elements in an array

Other operators and functions are context sensitive as well:

$x   = 'abc' =~ /(\w+)/;  # $x = 1
($x) = 'abc' =~ /(\w+)/;  # $x = 'abc'
@x   = localtime();       # (seconds, minutes, hours...)
$x   = localtime();       # 'Thu Dec 18 10:02:17 2008'

How an operator (or function) behaves in a given context is up to the operator. There are no general rules for how things are supposed to behave.

You can make your own subroutines context sensitive by using the wantarray function to determine the calling context. You can force an expression to be evaluated in scalar context by using the scalar keyword.

In addition to scalar and list contexts you'll also see "void" (no return value expected) and "boolean" (a true/false value expected) contexts mentioned in the documentation.

like image 24
Michael Carman Avatar answered Nov 17 '22 20:11

Michael Carman