Rosetta Code/Count examples: Difference between revisions
no edit summary
m (→{{header|D}}: Using works with template) |
No edit summary |
||
Line 61:
Stderr (client.getResponse);
}
}
</lang>
=={{header|Perl}}==
<lang Perl>
#!/usr/bin/perl -w
use strict ;
use LWP::UserAgent ;
use HTML::Parser ;
use constant DOCROOT => "http://www.rosettacode.org/wiki" ;
use constant SOLUTIONROOT => "http://www.rosettacode.org/w/index.php?title=" ;
my %tasklist = ( ) ; #key: last part of solution list URL, value: title of solution
my $ua = new LWP::UserAgent ;
my $url = DOCROOT . "/Category:Programming_Tasks" ;
my $request = HTTP::Request->new( 'GET' => "$url" ) ;
my $response = $ua->request( $request ) ;
my $counted = 0 ;
my $total_examples = 0 ;
my $solresponse ;
my $p = HTML::Parser->new( api_version => 3 ) ; #parser for list of tasks
my $q = HTML::Parser->new( api_version => 3 ) ; #parser for solutions by task
$p->handler( start => \&process , "tagname , attr" ) ;
$q->handler( text => \&langfinder, "text" ) ;
if ( $response->is_success( ) ) {
$p->parse( $response->content( ) ) ;
foreach my $task( keys %tasklist ) {
$request->uri( SOLUTIONROOT . "$task" . "&action=edit" ) ;
$solresponse = $ua->request( $request ) ;
if ( $solresponse->is_success( )) {
$q->parse( $solresponse->content( ) ) ;
if ( $tasklist{$task} ) {
print "$tasklist{$task} : $counted examples!\n" ;
}
$counted = 0 ;
$q->eof( ) ;
}
else {
print "Error: " . $solresponse->code( ) . " " . $solresponse->message( ) . "\n" ;
}
}
$p->eof( ) ;
print "\nTotal: $total_examples examples.\n" ;
}
else {
print "Error " . $response->code( ) . " " . $response->message( ) . "\n" ;
}
sub process( ) {
return if shift ne "a" ;
my $props = shift ;
if ( $props->{href} && $props->{href} =~ m,/wiki/([^:]+), ) {
if ( $1 !~ /Category/ ) {
$tasklist{ $1 } = $props->{title} ;
}
}
}
sub langfinder( ) {
my $text = shift ;
while ( $text =~ /header\|.+\}/g ) {
$counted++ ;
$total_examples++ ;
}
}
</lang>
Line 152 ⟶ 214:
This is the ScreenScrape class imported in the above class.
|