Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
List context is a powerful and sometimes subtle feature of Perl. In Perl, many operations and functions can return different results depending on the context they're called in. The two primary contexts are scalar and list. This tutorial focuses on list context sensitivity.
In Perl, context refers to the kind of value that is expected. This can affect the return value of expressions, functions, and operations:
=
operatorIn a list context, the assignment operator (=
) returns the list that was assigned, which can be used in further operations:
my @fruits = ("apple", "banana", "cherry"); my @copy = (@fruits); # @copy now has ("apple", "banana", "cherry")
localtime
functionThe localtime
function returns different values depending on its context:
my $time = localtime; # Scalar context: "Thu Apr 14 12:34:56 2023" my @time = localtime; # List context: (56, 34, 12, 14, 3, 123, 4, 104, 0)
Arrays in scalar context return their size:
my @numbers = (1, 2, 3, 4, 5); my $size = @numbers; # $size is 5
wantarray
The wantarray
function can tell if you're in list or scalar context, and you can adjust return values based on that:
sub example { if (wantarray) { return (1, 2, 3); # List context } else { return 1; # Scalar context } } my @arr = example(); # (1, 2, 3) my $scalar = example(); # 1
Be wary of accidentally using scalar context:
my @numbers = (1, 2, 3, 4, 5); my $wrong = (@numbers)[3]; # This is wrong; $wrong will be 4, not 5
You're accessing the array in scalar context first, which gives its size, and then you're trying to get the fourth element of the number 5, which doesn't make sense.
If you ever want to ensure list context, you can use parentheses:
my @colors = qw(red blue green); print scalar(@colors), "\n"; # Prints "3" print scalar( () = @colors), "\n"; # Also prints "3", but in a more forced way
Understanding context, especially list context sensitivity, is crucial in Perl. It allows for more concise and versatile code, but it also requires careful attention to avoid unexpected behavior. By being aware of how different functions and operations behave in list and scalar contexts, you can write more efficient and readable code.
Perl list context sensitivity:
my @fruits = ('apple', 'banana', 'orange'); my $fruit_count = @fruits; # Scalar context my @fruit_slice = @fruits; # List context
Scalar and list context in Perl:
my $count = @array; # Scalar context my @list = @array; # List context
Context sensitivity in Perl expressions:
my @numbers = (1, 2, 3); my $last_number = @numbers; # Scalar context, $last_number is 3 my @all_numbers = @numbers; # List context, @all_numbers is (1, 2, 3)
List assignment in scalar context Perl:
my $count = ($first, $second, $third) = (1, 2, 3); # $count is 3
How Perl handles list context:
my $scalar_context = scalar @array; my @list_context = @array;
Scalar vs. list context examples in Perl:
my $count = @array; # Scalar context my ($first, $second) = @array; # List context
Context-aware functions in Perl:
my $count = grep { $_ > 5 } @numbers; # Scalar context, $count is the number of elements > 5 my @selected_numbers = grep { $_ > 5 } @numbers; # List context, @selected_numbers has elements > 5
Perl subroutine context sensitivity:
my $result_scalar = get_value(); # Scalar context my @result_list = get_value(); # List context
Perl built-in functions and list context:
my $file_size = -s 'example.txt'; # Scalar context my @file_lines = <FILE_HANDLE>; # List context, reads all lines into an array