Leser: 19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/perl
use strict;
use warnings;
use HTML::Parser;
use Data::Dumper qw/Dumper/;
sub parse_links {
my $text = shift or die('missing text');
my @links = ();
my $p = HTML::Parser->new();
$p->handler(start => \&start_handler,"tagname,attr,self");
$p->parse($text);
foreach my $link(@links){
print "Linktext: ",$link->[1],"\tURL: ",$link->[0],"\n";
}
sub start_handler{
return if(shift ne 'a');
my ($class) = shift->{href};
my $self = shift;
my $text;
$self->handler(text => sub{$text = shift;},"dtext");
$self->handler(end => sub{push(@links,[$class,$text]) if(shift eq 'a')},"tagname");
}
} # /parse_links
1 2 3 4
$p->handler(start => $start_handler,"tagname,attr,self"); my $start_handler = sub { # sub wie gehabt };
2011-01-13T13:41:26 pqCode (perl): (dl )1 2 3 4$p->handler(start => $start_handler,"tagname,attr,self"); my $start_handler = sub { # sub wie gehabt };