Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Unexpected behaviour of a tied array (Bug in ActiveState Perl?)

by larsen (Parson)
on Nov 05, 2002 at 15:59 UTC ( #210487=perlquestion: print w/replies, xml ) Need Help??

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

I was playing with tied arrays when I found a strange behaviour. I tried the code (which you can see below) both with Perl 5.6.0 and with 5.8.0 on my MacOS X box, and it runs without surprises. With ActiveState 5.6.1 build 633, though, it leads to the following problem: if I use print $_; print "\n" to print the values, the program's output is:
Now I'm fetching the element with index: 0
Perl
Now I'm fetching the element with index: 1
Monks
On 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
Monks
So, 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
    Some more fuel for the fire:

    The following code gives the same double result

    ... my $o = ""; foreach ( @array ) { $o .= $_.' '; } print $o."\n";
    whereas omitting the .' ' as in the code below doesn't so its not part of the "" mechanism exactly.
    ... my $o = ""; foreach ( @array ) { $o .= $_; } print $o."\n";
    If you modify your FETCH routine (more code below) to return an integer count for each time its called then you see that the calls used are the second ones. It is bizarre why Activestate should call the routine twice but it looks to me like a BUG as if you were using a more bizarre backend such as a network socket you'd be missing data.
    my $c = 0; sub FETCH { my $self = shift; my $index = shift; $c++; print STDERR "Call ($c) fetching the element with index: $index\n" +; return $self->[ $index ].$c; }
    produces
    Call (1) fetching the element with index: 0
    Call (2) fetching the element with index: 0
    Call (3) fetching the element with index: 1
    Call (4) fetching the element with index: 1
    Perl2 Monks4 

    Dingus


    Enter any 47-digit prime number to continue.
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
    At first I thought it was due to the ALIASING magic that a foreach(@array) does, but it turns out, it has nothing to do with aliasing.

    It has to do with string interpolation. If you interpolate our magical $_ in a string, and there is nothing before the "$_", then two FETCHs are performed.

    I'm glad this is fixed after 5.6 (works fine in 5.7)

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Unexpected behaviour of a tied array (Bug in ActiveState Perl?)
by robartes (Priest) on Nov 05, 2002 at 16:09 UTC
    Not much help, but your code produces the expected output (only one FETCH) on my Linux Perl build (Perl 5.8.0, glibc 2.2). You might indeed be looking a an Active Perl only issue.

    CU
    Robartes-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://210487]
Approved by robartes
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2022-12-08 03:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?