Jun 8, 2013, 2:52 PM
Post #6 of 6
Your approach to obtain the size of an array is perfectly sound and valid, provided you do the following correction (already mentionned) :
Re: [2teez] size of an array when use strict is involved... a simple question
[In reply to]
(i.e. changing $foo to @foo). Or even, if you want to be more explicit on the context conversion you are really doing:
my $size = scalar @foo;
This does not make a real difference, but this states more clearly that you are using an array in a scalar context to obtain the number of elements of this array.
And, by the way, this code:
used without the "use strict" pragma did not report any compilation error, but certainly did not work properly (because $foo and @foo are two different variables). So, using strict tells you about a bug in your program that would or might not have been seen otherwise, this is perfect example of why using struct is so useful (even though, in this specific case, you did not understand the real meaning of the compilation error being reported, which is not very surprising, you need a bit of experience to understand the link between what the compiler is telling you and what is really wrong in your code).