larsen has asked for the wisdom of the Perl Monks concerning the following question:
Now I'm fetching the element with index: 0 Perl Now I'm fetching the element with index: 1 MonksOn the other hand, if I use print "$_\n" the output is:
Now I'm fetching the element with index: 0 Now I'm fetching the element with index: 0 Perl Now I'm fetching the element with index: 1 Now I'm fetching the element with index: 1 MonksSo, it seems it enters the sub FETCH twice, when I use $_ in a string. As I already said, the behaviour is the same (the first one behaviour, to be clear), when I try the code with different versions of Perl on MacOS X. Now, the code:
#!/usr/bin/perl use strict; use warnings; package Dummy; sub TIEARRAY { my $class = shift; return bless [], $class; } sub STORE { my $self = shift; my $index = shift; my $value = shift; return ($self->[ $index ] = $value ); } sub FETCH { my $self = shift; my $index = shift; print STDERR "Now I'm fetching the element with index: $index\n"; return $self->[ $index ]; } sub FETCHSIZE { my $self = shift; return scalar( @$self ); } package main; my @array; tie @array, 'Dummy'; $array[0] = 'Perl'; $array[1] = 'Monks'; foreach ( @array ) { print "$_\n"; # print $_; print "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unexpected behaviour of a tied array (Bug in ActiveState Perl?)
by dingus (Friar) on Nov 05, 2002 at 16:23 UTC | |
|
Re: Unexpected behaviour of a tied array (Bug in ActiveState Perl?)
by fglock (Vicar) on Nov 05, 2002 at 16:19 UTC | |
|
Re: Unexpected behaviour of a tied array (Bug in ActiveState Perl?)
by PodMaster (Abbot) on Nov 06, 2002 at 09:38 UTC | |
|
Re: Unexpected behaviour of a tied array (Bug in ActiveState Perl?)
by robartes (Priest) on Nov 05, 2002 at 16:09 UTC |