Skip to content

Instantly share code, notes, and snippets.

@kraih
Created June 2, 2011 07:35
Show Gist options
  • Save kraih/1004076 to your computer and use it in GitHub Desktop.
Save kraih/1004076 to your computer and use it in GitHub Desktop.
use 5.14.0;
use Mojo::DOM;
# Let's start with something simple
my $dom = Mojo::DOM->new('<div id="a">A</div>');
# All child elements now have accessors you can use instead of CSS3 selectors
say $dom->div->text;
# You also get direct hash access to element attributes
say $dom->div->{id};
# How about prepending another element
$dom->div->prepend('<div id="a">A</div>');
say "$dom";
# And modify the now-second one to fit in better
$dom->div->[1]->replace_content('B');
$dom->div->[1]->{id} = 'b';
say "$dom";
# Maybe add another nested element to the content of the first one
$dom->div->[0]->append_content('<p>C</p>');
say $dom->div->[0]->p->text;
say "$dom";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment