CARVIEW |
- #scalar EXPR
-
Forces EXPR to be interpreted in scalar context and returns the value of EXPR.
my @counts = ( scalar @a, scalar @b, scalar @c );
There is no equivalent operator to force an expression to be interpolated in list context because in practice, this is never needed. If you really wanted to do so, however, you could use the construction
@{[ (some expression) ]}
, but usually a simple(some expression)
suffices.Because
scalar
is a unary operator, if you accidentally use a parenthesized list for the EXPR, this behaves as a scalar comma expression, evaluating all but the last element in void context and returning the final element evaluated in scalar context. This is seldom what you want.The following single statement:
print uc(scalar(foo(), $bar)), $baz;
is the moral equivalent of these two:
foo(); print(uc($bar), $baz);
See perlop for more details on unary operators and the comma operator, and perldata for details on evaluating a hash in scalar context.
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via the GitHub issue tracker or email regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the Perl issue tracker, the mailing list, or IRC to report any issues with the contents or format of the documentation.