View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Terrence Brannon > HTML-Element-Replacer > HTML::Element::Replacer



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.01   Source   Latest Release: HTML-Element-Replacer-0.08


  HTML::Element::Replacer - simplify the HTML::Element clone() - push_content() ritual


  use HTML::Element::Replacer;

   my $replacer = HTML::Element::Replacer->new(look_down => [ scla => 'mid' ]);
   for my $data (@data) {
     $replacer->push_clone->defmap(attr_name => $data); # clone and push onto @temp_list
  # by default Replacer replaces... so it removes the element you were push_clone()ing


Let's say you have this HTML:


   <tr scla="top"/> 

   <tr scla="mid"> 
     <td kmap="brand"> blah </td>
     <td kmap="age"> blah </td>

   <tr scla="bot"/> 


Now let's say you have 5 data rows that you wish to display using the middle tr as your sample. The pure HTML::Tree way to do this would be:

 my $sample_tr = $tree->look_down(scla => 'mid');

 my @c;
 for my $data (@data) {
    my $c = $sample_tr->clone; 
    $c->defmap(kmap => $data);
    push @c, $c;


We did cheat a bit by using defmap() from HTML::Element::Library. Now, with this class, we can do this:

    my $replacer = HTML::Element::Replacer->new(look_down => [ scla => 'mid' ]);
    for my $data (@data) {
        my $clone = $replacer->push_clone->defmap(kmap => $data); # clone and push onto @temp_list

 } # replacer goes out of scope and then replaces sample_tr


Terrence Brannon, <tbone at>

Many thanks to Dave Rolsky in #moose on

  [16:04] @autarch: metaperl: I'm not sure you understand what "going out of scope" means
  [16:05] @autarch: { my $x = { foo => 1 }; my $y = { x => $x }; }
  [16:05] @autarch: both $x & $y go out of scope at the end of the block
  [16:05] @autarch: my $x = { foo => 1 };{  my $y = { x => $x }; }
  [16:05] @autarch: $y goes out of scope at the end of the block, $x persists
  [16:06] @autarch: my $x = { foo => 1 };{  my $y = {}; $x->{y} = $y }
  [16:06] @autarch: $y does not go out of scope until $x does
  [16:06] @autarch: the only thing that matters is references _to_ the thing in question, not references it holds
  [16:07] metaperl: ok that answers my question perfectly.... it doesnt matter if the hashref points to something that will remain 'alive' due to being defined in larger scope. thank you. now if $y were blessed into a class, when it goes out of scope... Classname::DESTROY($self) would be called .... where $self is $y in this case?
  [16:08] metaperl: because manually calling $object->finish is not elegant
  [16:08] metaperl: I want to handle that in a destructor method



Please report any bugs or feature requests to bug-html-element-replacer at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc HTML::Element::Replacer

You can also look for information at:



Copyright 2009 Terrence Brannon, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: