Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shouldn't accessing @Whatever::whatever produce at least a warning instead of an empty array?

Tags:

variables

perl

In the following code:

use strict;
use warnings;
use Data::Dumper;

my %hash = %Whatever::whatever;
my @array = @Whatever::whatever;
print Dumper \@array;
print Dumper \%hash;

My understanding is that @Whatever::whatever is accessing the symbol table, and doesn't produce an error message because symbol table is a hash. But why there isn't at least a warning message for accessing a non-existing element?

like image 845
Nylon Smile Avatar asked May 18 '12 04:05

Nylon Smile


1 Answers

Because it's almost impossible to catch a global variable in a state of non-existence in Perl. As soon as you mention one by name — even just to take a reference to it — it exists. And because arrays and hashes are different from scalars; a scalar comes into existence holding the value undef, which triggers a "use of uninitialized value" warning when used for most purposes; but arrays and hashes come into existence as empty arrays and hashes, and an empty array or hash isn't exceptional enough to warn about!

like image 198
hobbs Avatar answered Oct 06 '22 20:10

hobbs