The documentation states that a list constant in scalar context currently returns the number of values, but this may change in the future. It also states that, to use a list constant as an array, place it in parentheses. So, what is the correct way to get the number of values in a list constant, and what am I doing wrong in the code below? I get the error:
Bareword found where operator expected at D:\Batch/list-const-error.pl line 5, near "@(INFOFIELDS"
(Missing operator before INFOFIELDS?)
syntax error at D:\Batch/list-const-error.pl line 5, near "@(INFOFIELDS"
use strict;
use warnings;
use constant INFOFIELDS => qw( filedate filetime filesize mtime filename );
use constant OUTTEMPLATE => ' A9 A7 A12 A12 A* ';
use constant NELEMENTS => scalar @(INFOFIELDS);
my $line = pack( OUTTEMPLATE, @ARGV[0..NELEMENTS-1] );
print( "$line\n" );
A constant can be used as a subroutine, so you can call
scalar INFOFIELDS()orscalar &INFOFIELDSto access the number of items in the constant list.