Leser: 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
... use HTML::Parser; my $parser = HTML::Parser->new; $parser->handler( start => \&start_handler, 'tagname, self' ); $parser->parse( $content ); sub start_handler { return if shift ne 'li'; my $self = shift; $self->handler( start => \&title_handler , 'tagname, attr' ); sub title_handler { return if shift ne 'a'; my $title = shift->{title}; return unless $title; if ( $title =~ /\Q$name\E/sm ) { say $title; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
use HTML::Parser; my $parser = HTML::Parser->new; $parser->handler( start => \&start_handler, 'tagname, self' ); $parser->parse( $content ); sub start_handler { return if shift ne 'li'; my $self = shift; $self->handler( start => \&title_handler , 'tagname, attr' ); } sub title_handler { return if shift ne 'a'; my $title = shift->{title}; return unless $title; if ( $title =~ /\Q$name\E/sm ) { say $title; } }
1 2 3 4 5 6 7 8
use XML::LibXML; my $parser = XML::LibXML->new; my $doc = $parser->parse_html_string( $content ); my @nodes = $doc->findnodes( '//li/a[@title]' ); for my $node ( @nodes ) { say $node->nodeName, " ", $node->string_value; }
my @nodes = $doc->findnodes( '//li/a[@title] =~ /regex/' );
1 2 3 4 5 6
use HTML::Query; ... my $q = HTML::Query->new( text => $text ); my @nodes = $q->query("li a[title=$title]");
1 2 3
use XML::Twig; my $t= XML::Twig->new( twig_handlers => { '//li/a[@title=~ /Bra/]' => sub { say $_->text } } ); $t->parsefile( 'temp_html.html');
1 2 3 4 5 6 7 8
use HTML::TreeBuilder::XPath; my $tree= HTML::TreeBuilder::XPath->new; $tree->parse_file( 'temp_html.html'); my @v=$tree->findnodes_as_strings( '//li/a[@title=~ /Bra/]' ); say for @v; $tree->delete;
1 2 3 4 5 6
use HTML::Query 'Query'; my @p = Query( file => 'temp_html.html', 'li a[title]' ); for ( @p ) { say $_->as_trimmed_text if $_->as_trimmed_text =~ /Bra/; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$parser->handler( start => \&start_handler, 'tagname, self' ); $parser->parse( $content ); sub start_handler { return if shift ne 'li'; my $self = shift; $self->handler( start => \&title_handler, 'tagname, attr, self' ); } sub title_handler { my( $tagname, $attr, $self ) = @_; return if $tagname ne 'a'; return unless $attr->{title}; my $text; $self->handler( text => sub{ $text = shift; }, 'dtext' ); say "$text: $attr->{href}"; # Use of uninitialized value $text in concatenation (.) or string }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
use Data::Dumper; my $parser->handler( start => \&start_handler, 'tagname, self' ); $parser->parse( $content ); print Dumper($parser->{_my_vars}); sub start_handler { return if shift ne 'li'; my $self = shift; $self->handler( start => \&title_handler, 'tagname, attr, self' ); } sub title_handler { my( $tagname, $attr, $self ) = @_; return if $tagname ne 'a'; return unless $attr->{title}; my $cnow=$self->{_my_vars}->{count} || 0; $self->{_my_vars}->{count}++; $self->handler( text => sub{ $self->{_my_vars}->[$cnow]->{text} =shift(); }, 'self, dtext' ); $self->{_my_vars}->[$cnow]->{href}=$attr->{href}; }
2010-03-01T19:10:12 betterworld... Verschachtelte Subroutinen gibt es in Perl auch gar nicht in dieser Form....
1 2 3 4 5 6 7 8 9 10 11 12 13
sub outer { my ($x) = @_; my $inner = sub { # $x ist hier sichtbar! } # inner kann man aufrufen... $inner->(...); # ...oder als Wert übergeben return $inner; }
QuoteUnd das find ich öfter mal sehr schade. Wär das nicht gut, wenn genau solche subs in einer sub z.B. den Namensraum der parent sub als globale Variablen wahrnehmen könnten ?
QuoteZusätzlich zu den global definierten an sich.
QuoteWenn man bissel TK macht hat man für jede Kleinigkeit schnell ein Rudel subs zusammen, die mit dem Rest des Programms nicht viel zu tun haben. Wäre praktisch, wenn diese dann bequemer auf gemeinsam zu nutzende Variablen zugreifen könnten, ohne exzessive Parameterübergabeorgien zu betreiben :)
1 2 3 4 5 6 7 8 9
{ # neuer lexikalischer block my ( $x, $y, $z ) = ( 42, 42+1, 42+2 ); sub foo { } sub bar { } }