scalar#
Force EXPR to be evaluated in scalar context and return its value.
scalar is not really an operator in its own right — it is a
context-coercion marker the parser recognises in front of a unary
expression. Perl evaluates the expression in scalar context and yields
whatever that expression produces there. The function exists because
most positions in a Perl program already impose a context (list or
scalar) on their operands, and occasionally you need to override
list context with scalar context explicitly.
Synopsis#
scalar EXPR
scalar @array
scalar %hash
scalar localtime
What you get back#
The value of EXPR as evaluated in scalar context. The exact shape
depends entirely on what EXPR is:
For an array, the element count.
For a hash, a true value (non-empty) or
0(empty); in older Perls this was a bucket-usage string, but from 5.26 onward it is the key count.For a list-context-sensitive function, whatever that function returns when called in scalar context (for example,
localtimereturns its ctime-style string instead of the nine-element list).For an already-scalar expression, the same value —
scalaris a no-op there.
Global state it touches#
None. scalar is a pure context cast; it does not read or write any
special variables of its own. The expression you pass in may, of
course, touch whatever globals it normally touches.
Examples#
Count the elements of an array inside a list-context position:
my @items = (10, 20, 30);
my @counts = (scalar @items); # (3)
print scalar @items, "\n"; # 3
Without scalar, @items would expand to its elements and
@counts would be (10, 20, 30).
Print the human-readable date from localtime. In list context
localtime returns nine fields; in scalar context it returns the
familiar ctime string:
print localtime, "\n"; # nine numbers run together
print scalar localtime, "\n"; # "Thu Apr 23 14:02:11 2026"
The same trap appears everywhere a builtin is list-context-sensitive:
my @parts = localtime; # (sec, min, hour, mday, ...)
my $now = scalar localtime; # ctime string
Test whether a hash is non-empty without allocating its key list:
if (scalar %config) {
# %config has at least one key
}
if (%config) already imposes scalar (boolean) context, so the
scalar here is redundant but explicit; writers sometimes include it
to make intent visible to readers.
Build a list of counts from several arrays in one expression:
my @counts = (scalar @a, scalar @b, scalar @c);
Coerce a function call that would otherwise splat into a surrounding list:
my @row = ($id, scalar get_tags($id), $mtime);
Without scalar, get_tags would run in list context and its
returned list would be flattened into @row alongside $id and
$mtime.
Edge cases#
No list-context counterpart. There is no
list EXPRoperator; list context is the default in almost every position where you might want to impose it. If you genuinely need to coerce a scalar expression into list context, the idiom is@{[ EXPR ]}, though in most cases plain(EXPR)is enough.Parenthesised list trap.
scalaris a unary operator. Passing it a parenthesised comma expression does not evaluate that expression as a list; it evaluates it as a scalar comma operator, which runs every sub-expression in void context except the last and returns the last in scalar context:print uc(scalar(foo(), $bar)), $baz; # equivalent to: foo(); # void context print(uc($bar), $baz); # $bar in scalar context
If you wanted
foo()’s return values included,scalar (list)is the wrong tool.Hash in scalar context. From Perl 5.26 onward,
scalar %hreturns the number of keys, the same asscalar keys %h. On older Perls it returned a string like"4/8"reporting bucket usage; pperl targets 5.42 and so always returns the key count.Subroutine prototypes. A subroutine declared with a scalar prototype (
($)) already imposes scalar context on its argument, soscalaris redundant in front of the call. A(@)or(\@)prototype does not, soscalarstill matters:sub count ($) { $_[0] } count @items; # @items in scalar context: 3
Redundant on already-scalar expressions.
scalar $x,scalar 42,scalar "text"are no-ops. Perl does not warn about them; they are occasionally used for emphasis but add nothing.Not a function call. Because
scalaris a parser-level context marker, you cannot take\&scalar, cannot override it with asub scalar { ... }, and cannot pass it as a code reference.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
wantarray— inspect the context you were called in, rather than impose one on a sub-expressiondefined— similarly a unary operator applied to an expression; useful alongsidescalarwhen testing function resultslocaltime— canonical example of a builtin whose return shape depends on contextreverse— also context-sensitive;scalar reverse $sreverses characters,reverse @areverses a listkeys—scalar keys %his the long form ofscalar %hwith the same result on 5.26+