Domanda

I can not understand the following snippet:

perl  -e '  
my @test = undef;  
if(@test) {print "Full\n";}'  
Full  

It seems that @test although it is assigned undef it actually has 1 element which is undef.
So what is the proper way to test if @test is null?
Doing if(scalar(@test) == 1 && !$test[0]) seems weird to me

È stato utile?

Soluzione

To initialize an @array to nothing, just do:

my @test = (); 

Currently you're doing:

my @test = (undef);

Which, as you are observing, is assigning the array a single value.

To clear an already declared array, you can use undef like so:

undef @test;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top