Re: Patch suggestion for HTML::Element::delete (Was: Deleting images

Andreas Koenig (k@anna.in-berlin.de)
Tue, 13 Aug 1996 18:58:38 +0200


>>>>> WWW projekt <wwwproj@dna.lth.se> writes:

 >> sub delete
 >> {
 >> $_[0]->delete_content;
 >> +     my $pos_within_parent;
 >> +     no overload;
 >> +     foreach (0..$#{$_[0]->{'_parent'}{'_content'}}) {
 >> +       # looking for myself in parent and splicing me out after
 >> +       if (ref($_[0]->{'_parent'}{'_content'}->[$_]) && 
 >> +	      "$_[0]->{'_parent'}{'_content'}->[$_]" eq "$_[0]"){
 >> +           $pos_within_parent = $_;
 >> +           last;
 >> +       }
 >> +     }
 >> +     if (defined $pos_within_parent) {
 >> +       splice @{$_[0]->{'_parent'}{'_content'}}, 
 >> +       $pos_within_parent, 1;
 >> +     }
 >> delete $_[0]->{'_parent'};
 >> delete $_[0]->{'_pos'};
 >> $_[0] = undef;
 >> 
 >> You like it, Stefan?

  > Yes, it should work.

Nope, sorry. The above code triggers a bug in perl and will give you
core dumps in many situation.

I've posted a rather dense variant of this code to perl5-porters for
further inspection. Until this is resolved, keep hands off this code!

  >                      I blush as I admit that I have never heard of
  > splice before, it would have solved my problems a long time ago and this
  > whole discussion would have been unecessary. Although, this result would
  > not have been if I had not been a perl newbie :-).

Dump the newbie sticker, you won't need it anymore ;-)

  > Thank you for your help.

You're welcome,

[...]

  > The pleasure is all mine ;-)
  > Anybody else have a suggestion of what a general 'insert' method would
  > look like?

  > The insert method I want should be added to Element.pm and the head of
  > the method should be something like:

  > sub insert
  > {
  >    my ($self, $insert_after, $insert_me) = @_;
  > ..

  > where $insert_after is a known element in $self's content and $insert_me
  > is the element to be inserted.

If there were a splice, all of shift, unshift, pop, and push could be
implemented as calls to splice. So maybe

  $number_of_removes = splice($self, $insert_after, $replace_how_many, @insert_these);

would be a good signature for the splice (to be most akin to its
cousin).

andreas