Rosetta Code/Count examples: Difference between revisions
Content deleted Content added
Line 905: | Line 905: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<lang Perl> |
<lang Perl>use LWP::Simple; |
||
#!/usr/bin/perl -w |
|||
use strict ; |
|||
use LWP::UserAgent ; |
|||
use HTML::Parser ; |
|||
⚫ | |||
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( ) ) { |
|||
my $list_url = "/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"; |
|||
$p->parse( $response->content( ) ) ; |
|||
foreach my $task( keys %tasklist ) { |
|||
for (get("$site$list_url") =~ /cm.*?title="(.*?)"/g) { |
|||
$request->uri( SOLUTIONROOT . "$task" . "&action=edit" ) ; |
|||
(my $slug = $_) =~ tr/ /_/; |
|||
my $count = () = get("$site/wiki/$slug") =~ /toclevel-1/g; |
|||
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( ) ; |
|||
⚫ | |||
} |
|||
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++ ; |
|||
} |
|||
} |
|||
⚫ | |||
{{libheader|Mojolicious}} |
{{libheader|Mojolicious}} |