develooper Front page | perl.perl5.porters | Postings from December 2004

Should grep() affect weakened refs?

Thread Next
From:
Elizabeth Mattijsen
Date:
December 4, 2004 01:17
Subject:
Should grep() affect weakened refs?
Message ID:
f0611042abdd72e7c10e0@[192.168.56.3]
While fixing some issues with ogd.pm (version 0.02 is now on its way 
to CPAN), I came along this phenomenon.  Take this little program:

===================================================
use Scalar::Util qw(isweak weaken);

my @a;
push @a,bless \$_,'Foo' foreach 0..2;
my @b;
weaken( $b[@b] = $_ ) foreach @a;

print "original:\n";
print " $$_: ".(isweak $_)."\n" foreach @b;

print "object 1 destroyed:\n";
$a[1] = undef;
print " $$_: ".(isweak $_)."\n" foreach @b;

print "cleanup performed:\n";
@b = grep { defined } @b;
print " $$_: ".(isweak $_)."\n" foreach @b;
===================================================

I would expect to generate the following output:
----------------------------------------------------------------
original:
  0: 1
  1: 1
  2: 1
object 1 destroyed:
  0: 1
  :
  2: 1
cleanup performed:
  0: 1
  2: 1
----------------------------------------------------------------

however, the result is (for all Perl versions from 5.8.0 through 5.8.6):
----------------------------------------------------------------
original:
  0: 1
  1: 1
  2: 1
object 1 destroyed:
  0: 1
  :
  2: 1
cleanup performed:
  0:
  2:
----------------------------------------------------------------

indicating that the weakened references after the grep() have become 
strong references.

Is this intentional?  Or a bug?  Or a side-effect of grep() that's 
just not easy to get around?



BTW, instead of "@b = grep { defined } @b", ogd.pm now uses the 
following yucky structure:

         foreach (reverse 0..$#b) {
             splice @b,$_,1 unless defined $b[$_];
         }

Suggestions for a less yucky approach are welcome.


Liz

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About