http://www.perlmonks.org?node_id=535403

AntsPants has asked for the wisdom of the Perl Monks concerning the following question:

I have a list, and sometimes I want to read-ahead (next element) in the list and compare it to my current element (while the index stays on my current element)

If there's a next element (i.e. I'm not out of bounds) then I'll grab a value. If there's no next element then I'm done.

This works ..........

my $next_version_key = $ordered_list->[($i+1)]->[0] if( $ordered_list->[($i+1)] );
but then I thought I'd try ............
my $next_version_key = $ordered_list->[($i+1)]->[0] || undef;
and that added ..........
[] to @{ $ordered_list }
which meant my list had grown. The next time round, it added ......
[ undef, {} ]
next time round ..............
[ ${\$VAR1->[3][0]}, {} ]
usw. The same happens if I use ............
my $next_version_key = $ordered_list->[($i+1)]->[0] || 0;
Now, it seems as though $ordered_list->[($i+1)]->[0] is evalutaing to true even though it's out-of-bounds but what strange behaviour!!

Can someone throw some light on that for me? This isn't critical as I'm using ...

my $next_version_key = $ordered_list->[($i+1)]->[0] if( $ordered_list->[($i+1)] );
which my prefered way .... I'm just interested in the internals with this prob.

Cheers -Ants