201 |
my $args = {@_}; |
my $args = {@_}; |
202 |
|
|
203 |
my $tree = $args->{tree} || die "no tree"; |
my $tree = $args->{tree} || die "no tree"; |
204 |
my $message = $args->{message}; |
my $message = $args->{message} || ''; |
205 |
my $fatal = $args->{fatal}; |
my $fatal = $args->{fatal}; |
206 |
die "no templates" unless defined( $args->{templates} ); |
die "no templates" unless defined( $args->{templates} ); |
207 |
my @templates = @{ $args->{templates} }; |
my @templates; |
208 |
|
if ( ref( $args->{template} ) eq 'ARRAY' ) { |
209 |
|
@templates = @{ $args->{templates} }; |
210 |
|
} else { |
211 |
|
@templates = ( $args->{templates} ); |
212 |
|
} |
213 |
|
|
214 |
push @templates, ( undef, undef ) if ( $#templates == 0 ); |
push @templates, ( undef, undef ) if ( $#templates == 0 ); |
215 |
|
|
217 |
( $#templates + 1 ) % 3 == 0 |
( $#templates + 1 ) % 3 == 0 |
218 |
); |
); |
219 |
|
|
220 |
my ( $result, $el, $attr, $value ); |
my ( $el, $attr, $value ); |
221 |
|
|
222 |
|
my @results; |
223 |
my @tags; |
my @tags; |
224 |
|
|
225 |
while ( @templates ) { |
while ( @templates ) { |
227 |
my $tag = $attr ? "<$el $attr=\"$value\">" : "<$el>"; |
my $tag = $attr ? "<$el $attr=\"$value\">" : "<$el>"; |
228 |
push @tags, $tag; |
push @tags, $tag; |
229 |
$self->log->debug("looking for $message $tag"); |
$self->log->debug("looking for $message $tag"); |
230 |
$result = $tree->look_down( '_tag', $el, sub { |
@results = $tree->look_down( '_tag', $el, sub { |
231 |
return 1 unless ( $attr && $value ); |
return 1 unless ( $attr && $value ); |
232 |
( $_[0]->attr( $attr ) || '' ) eq $value; |
( $_[0]->attr( $attr ) || '' ) eq $value; |
233 |
}); |
}); |
234 |
last if $result; |
last if @results; |
235 |
} |
} |
236 |
|
|
237 |
if ( ! $result ) { |
if ( ! @results ) { |
238 |
my $msg = "can't find $message ", join(" ", @tags); |
my $msg = "can't find $message ", join(" ", @tags); |
239 |
die $msg if ( $fatal ); |
die $msg if ( $fatal ); |
240 |
warn $msg; |
warn $msg; |
241 |
return; |
return; |
242 |
} |
} |
243 |
|
|
244 |
return $result; |
$self->log->debug("found ", $#results + 1, " results"); |
245 |
|
|
246 |
|
return @results if wantarray; |
247 |
|
return shift @results; |
248 |
} |
} |
249 |
|
|
250 |
sub scrape { |
sub scrape { |