ZetCode

Perl basics: scalars, sigils, and context

last modified October 8, 2026

Perl's sigils show how a variable is used, while context determines whether an expression supplies one scalar value or a list of values. Understanding both ideas makes Perl assignments, function calls, and return values easier to read.

Scalar values

A scalar holds one value, such as a number, string, or reference. Scalar variables begin with $. The sigil on an array or hash element is also $, because the selected element is one scalar.

scalars.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my $count = 3;
my $title = 'Perl basics';
my @items = ('map', 'sort', 'grep');
my %prices = (tea => 4, coffee => 5);

say $count;
say $title;
say $items[0];
say $prices{tea};

@items refers to the array, but $items[0] refers to one element. Likewise, %prices is the hash and $prices{tea} is one value.

$ perl scalars.pl
3
Perl basics
map
4

Sigils and variable forms

The sigil is tied to the value used by an expression, not just the variable's name. An array element is scalar; a slice returns a list of selected elements.

List context and scalar context

Perl expressions are evaluated in a context supplied by their surroundings. List context asks for zero or more values. Scalar context asks for one value. The same expression can behave differently in each context.

An array in list context provides its elements. In scalar context, it provides its number of elements.

context.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my @values = (10, 20, 30);
my @copy = @values;
my $count = @values;

say "values: @copy";
say "count: $count";

The array assignment on the second line requests list context; assigning to $count requests scalar context. Write scalar @values when an explicit context conversion improves clarity.

$ perl context.pl
values: 10 20 30
count: 3

Context changes operators

Some operators return different kinds of values depending on context. For example, reverse reverses the order of a list in list context and reverses the characters of a string in scalar context.

reverse_context.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my @words = ('red', 'green', 'blue');
my @reversed_list = reverse @words;
my $reversed_string = scalar reverse 'desserts';

say "@reversed_list";
say $reversed_string;

The built-in localtime is another context-sensitive function: in list context it returns date and time fields; in scalar context it returns a formatted date string.

$ perl reverse_context.pl
blue green red
stressed

Hash context

A hash in list context yields a flat list of key-value pairs, but the order of those pairs is not defined. Avoid relying on hash iteration order; sort the keys when you need a predictable display.

Do not use scalar %hash to count entries. Its scalar value is an implementation-oriented bucket status, not the number of key-value pairs. Use scalar keys %hash instead.

hash_context.pl
use strict;
use warnings;
use v5.30.0;

my %scores = (Ada => 91, Grace => 96);
my $number_of_scores = scalar keys %scores;
say $number_of_scores; # 2

The example counts the number of key-value pairs in the hash. The keys function returns a list of the hash's keys, and scalar converts that list to a count.

$ perl hash_context.pl
2

Functions receive context

Context also affects a subroutine's return expression. In list context, an expression may return multiple values; in scalar context, it must produce one scalar result. Use wantarray only when a subroutine genuinely needs to distinguish the caller's context.

See Perl variables for lexical variables and special variables, and Perl subroutines for arguments and return values.

Source

Perl data types ยท Perl operators and context

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.