Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more modern Mojo::DOM examples
  • Loading branch information
kraih committed Jul 3, 2012
1 parent 497c342 commit 44bf388
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Collection.pm
Expand Up @@ -80,7 +80,7 @@ sub sort {
sub uniq {
my $self = shift;
my %seen;
return $self->new(grep { !$seen{$_}++ } @$self);
return $self->grep(sub { !$seen{$_}++ });
}

1;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -632,11 +632,11 @@ Turns message body into a L<Mojo::DOM> object and takes an optional selector
to perform a C<find> on it right away, which returns a collection.
# Perform "find" right away
$message->dom('h1, h2, h3')->each(sub { say $_->text });
$message->dom('h1, h2, h3')->pluck('text')->join("\n")->say;
# Use everything else Mojo::DOM has to offer
say $message->dom->at('title')->text;
$message->dom->html->body->children->each(sub { say $_->type });
$message->dom->html->body->children->pluck('type')->join("\n")->say;
=head2 C<error>
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -549,7 +549,7 @@ Mojo::UserAgent - Non-blocking I/O HTTP 1.1 and WebSocket user agent
# Scrape the latest headlines from a news site
$ua->max_redirects(5)->get('www.reddit.com/r/perl/')
->res->dom('p.title > a.title')->each(sub { say $_->text });
->res->dom('p.title > a.title')->pluck('text')->join("\n")->say;
# IPv6 PUT request with content
my $tx
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/dom.t
Expand Up @@ -14,7 +14,7 @@ my $dom = Mojo::DOM->new->parse(
'<div><div FOO="0" id="a">A</div><div id="b">B</div></div>');
is $dom->at('#b')->text, 'B', 'right text';
my @div;
$dom->find('div[id]')->each(sub { push @div, shift->text });
push @div, $dom->find('div[id]')->pluck('text')->each;
is_deeply \@div, [qw(A B)], 'found all div elements with id';
@div = ();
$dom->find('div[id]')->each(sub { push @div, $_->text });
Expand Down Expand Up @@ -301,7 +301,7 @@ is $dom->replace(''), '', 'right result';
is "$dom", '', 'right result';
$dom->replace('<div>foo<p>lalala</p>bar</div>');
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
$dom->find('p')->each(sub { shift->replace('') });
$dom->find('p')->pluck(replace => '');
is "$dom", '<div>foobar</div>', 'right result';
$dom = Mojo::DOM->new->parse('<div>♥</div>');
$dom->at('div')->replace_content('');
Expand Down

0 comments on commit 44bf388

Please sign in to comment.