How do you use context (scalar vs list) with a short example?

In Perl, context refers to the environment in which a variable or expression is evaluated. There are two primary types of context: scalar context and list context.

Scalar context is used when a single value is expected, while list context is used when multiple values might be returned.

Here is a short example demonstrating the difference:

# Define a subroutine that returns a list sub example { return (1, 2, 3); } # Scalar context my $scalar_value = example(); # Only the first value (1) is returned print "In scalar context: $scalar_value\n"; # List context my @list_value = example(); # All values (1, 2, 3) are returned print "In list context: @list_value\n";

Perl context scalar context list context programming