perl - Self-deleting array elements (once they become undefined) -


i have perl script generating array of weak references objects. once 1 of these objects goes out of scope, reference in array become undefined.

ex (pseudo code):

# imagine array of weak references objects @array = ( $obj1_ref, $obj2_ref, $obj3_ref );  # other code here causes last strong reference # of $obj2_ref go out of scope.  # have following array @array = ( $obj1_ref, undef, $obj3_ref ) 

is there way make undefined reference automatically remove array once becomes undefined?

i want @array = ($obj1_red, $obj3_ref ).

edit:

i tried solution , didn't work:

#!/usr/bin/perl use strict; use warnings;  {     package object;     sub new { $class = shift; bless({ @_ }, $class) } }  {     use scalar::util qw(weaken);     use data::dumper;      $object = object->new();      $array;     $array = sub { \@_ }->( grep defined, @$array );      {         $object = object->new();         @$array = ('test1', $object, 'test3');         weaken($array->[1]);         print dumper($array);     }      print dumper($array); 

output:

$var1 = [           'test1',           bless( {}, 'object' ),           'test3'         ]; $var1 = [           'test1',           undef,           'test3'         ]; 

the undef not removed array automatically.

am missing something?

edit 2:

i tried removing undefined values array in destroy method of object, doesn't appear work either. appears since object still technically not "destroyed" yet, weak references still defined until destroy method completed...

no, there isn't, short of using magical (e.g. tied) array.

if have reference array instead of array, can use following filter out undefined element efficiently without "hardening" of references.

$array = sub { \@_ }->( grep defined, @$array ); 

this doesn't copy values @ all, in fact. "c pointers" copied.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -