- Perl ›
- builtin functions ›
- here
scalar function - evaluated in scalar context
You can use the scalar function to force scalar context.
# Evaluate in scalar context scalar executable statement
For example, if you evaluate array in a scalar context, you can get the number of arrays, but if you write it explicitly using the scalar function, it will be as follows.
# Number of arrays my $count = scalar @values;
Do I have to use the scalar function?
If you devise a program, you can write a program without using the scalar function.
For example, if you want to pass a value to the argument of subroutine, pass it via variable.
# Pass the number of arrays directly foo (scalar @values); # Save the number of arrays in a scalar variable before passing my $count = @values; foo ($count);
Good variable names make the program self-explanatory and easy to read. In many cases, it's better to assign to a variable once and then use it elsewhere, rather than using the scalar function.