my %order;
while ( my $rec = $data->fetchrow_hashref ) {
push @{ $result{ $rec->{"ID"} } }, $rec->{"item"};
push @order, $rec->{ID};
}
I get Global symbol "@order" requires explicit package name at linepush @order, $rec->{ID};
my %order;
while ( my $rec = $data->fetchrow_hashref ) {
push @{ $result{ $rec->{"ID"} } }, $rec->{"item"};
push @order, $rec->{ID};
}
I get Global symbol "@order" requires explicit package name at linepush @order, $rec->{ID};
Perl's sigils identify distinct data types. And two identical identifiers are different variables entirely if they have different sigils.
my $var; # This is a scalar.
my @var; # This is an array.
my %var; # This is a hash.
Each of those three are completely different variables.
The error message you are getting is because in line one of the code you posted you declare a hash named %order, while on line four of the code you posted, you push to an array named @order. That array has never been declared. Without an explicit declaration indicating otherwise, Perl will assume the first time it sees a variable that it's intended to be a package global. And because you're using strict 'vars', or strict (where vars is implicit`), Perl doesn't let you autovivify a package global, or any other type of variable, without first declaring it unless you fully qualify its name.
This behavior is explained in perldoc strict, where it states:
This generates a compile-time error if you access a variable that was neither explicitly declared (using any of my, our, state, or use vars ) nor fully qualified.
Since the clear intent in your code is to push values onto an array, it's probable that the simplest fix is to change your first line from my %order; to my @order;, so that you're declaring an array rather than a hash.
It's unclear, without seeing more code, to know what to do with the line where you're pushing onto an array by reference, though. Presumably you already know that part of the code to be correct.