[Israel.pm] why does the array behave cyclic?
Sagiv Barhoom
sagivba at 012.net.il
Sat Jul 1 17:02:33 EEST 2006
hi all, I am playing with ref to subroutings and wrote the folowing code:
sub get_list_API_of {
my @al= @_;
my $index=0;
my %LIST;
$LIST{prev}= sub{(defined $al[--$index])?$al[$index]:undef};
$LIST{head}= sub{$al[0]};
$LIST{tail}= sub{$al[$#al]};
$LIST{curr}= sub{$al[$index]};
$LIST{get_index}= sub {$index};
return \%LIST;
} # ---------- end of subroutine get_list_API_of ----------
#######################################
# TEST
#######################################
my @array=qw{a b c d e f g};
my $L=get_list_API_of @array;
$\="\n";
print "curr: ".$L->{curr}->()." index: ".$L->{get_index}->();
print "prev: ".$L->{prev}->()." index: ".$L->{get_index}->();
print "prev: ".$L->{prev}->()." index: ".$L->{get_index}->();
print "prev: ".$L->{prev}->()." index: ".$L->{get_index}->();
print "tail: ".$L->{tail}->()." index: ".$L->{get_index}->();
print "head: ".$L->{head}->()." index: ".$L->{get_index}->();
this is the output:
[sagivba at black ~]$ perl itr1.pl
curr: a index: 0
prev: g index: -1
prev: f index: -2
prev: e index: -3
tail: g index: -3
head: a index: -3
can anyone explain why it retuen g at the second line
(the index is -1 shouldn't the sub return undef )?
Regard Sagiv
More information about the Perl
mailing list