From: | FOOLISH [...] cpan.org |
Subject: | I'd like to be able to accumulate grabbed things |
#vim:ft=perl
use Test::More;
use Scrappy ':syntax';
my $expected_critters = [
{
'views' => '69',
'display_name' => 'Sexy Coder',
'username' => 'sexycoder'
},
{
'views' => '22',
'display_name' => 'Lady Feline',
'username' => 'ladyfeline'
},
{
'views' => '0',
'display_name' => 'Pesky',
'username' => 'peskycat'
},
{
'views' => '1',
'display_name' => 'Nobody Cares',
'username' => 'nobodycares'
},
];
my $html = sprintf "<html><ul>%s</ul></html>",
join "\n", map {
my ($user,$name, $views) = @{ $_ }{ qw[ username display_name views ]};
$s=$views == 1 ? '' : 's';
qq[
<li>
<a href="/~${user}">View <span class="username">${name}</span>'s profile</a>
(viewed <span class="views">$views</span> time$s)
</li> ]# vim's confused!
} @$expected_critters;
# diag "the html looks like this: $html or something";
var critters => grab '//li', sub {
my %critter;
@critter{ qw[ display_name views ]} =
map $_->as_text,
$_[0]->look_down(_tag => 'span');
$_[0]->look_down (_tag => 'a', sub {
($critter{ username }) = shift->attr('href') =~ /~(\w+)/
});
\%critter;
}, $html;
is_deeply(
$expected_critters,
var->{critters},
"It'd be great if grab accumunated stuff returned by the callback when var'ed"
);
grab '//li', sub {
my %critter;
@critter{ qw[ display_name views ]} =
map $_->as_text,
$_[0]->look_down(_tag => 'span');
$_[0]->look_down (_tag => 'a', sub {
($critter{ username }) = shift->attr('href') =~ /~(\w+)/
});
var more_critters => [
@{ var->{more_critters} || [] },
\%critter
];
}, $html;
is_deeply( $expected_critters,
var->{more_critters},
"I can accumulate them by myself, but it's pesky"
);