411 REFERENCE · Wikibooks
Perlwikibot/3.0 release
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
AA0000 Important note
This release includes breaking changes. These release notes will guide you through the process of updating your calling code.
The 3.0 release marks a major transition. For starters, all methods use the API where possible. This means that return formats have changed in some cases. Likewise, some method calls have changed. Some of these still accept a backward-compatible form; this will be noted where applicable.
__TOC__
Breaking changes
All methods
Overview
Nearly all methods now return undef on failure.
Rationale
Separating errors from returned data is important. If you require the error code or error details, they are available in $bot->{'error'}->{'code'} and $bot->{'error'}->{'details'} respectively.
Impact
Any method calls which did not ignore the return value must now check for undef.
Example: get_text() and get_pages()
These methods now return undef for non-existent pages instead of an error code. per above Any special cases you had for handling error codes must now look for undef instead. Calls involving pages which exist are unaffected. my $text = $bot->get_text('Non-existent page');
print $text if defined($text);
my @pages = ('Page 1', 'Page 2', 'Page 3');
my $thing = $bot->get_pages(\@pages);
foreach my $page (keys %$thing) {
my $text = $thing->{$page};
print "$text\n" if defined($text);
} my $text = $bot->get_text('Non-existent page');
print $text unless ($text eq "2");
my @pages = ('Page 1', 'Page 2', 'Page 3');
my $thing = $bot->get_pages(\@pages);
foreach my $page (keys %$thing) {
my $text = $thing->{$page};
print "$text\n" unless ($text eq "2");
}
login()
This method now returns true on success; false on failure. In Perl, non-zero is true and means success. Even the syscall wrappings in Perl follow this convention. All calls to login() which checked return values must be updated. $bot->login({
username => "Mike's bot account",
password => $password,
}) or die "Couldn't log in"; my $failure = $bot->login("Mike's bot account", $password);
die "Couldn't log in" if $failure;
linksearch()
This method now uses keys 'url' and 'title' This reduces confusion in cases where the programmer wishes to use a callback hook to do incremental processing. Without this change, accessing data would have been done with two different sets of keys. Any call needs to have the key names updated. my $options = { max => 10, }; # I only want some results
my @links = $bot->linksearch("slashdot.org", 1, undef, $options);
foreach my $hash (@links) {
my $url = $hash->{'url'};
my $page = $hash->{'title'};
print "$page: $url\n";
}
# Use a callback:
my $options = { hook => \&mysub, }; # I want to do incremental processing
$bot->linksearch("slashdot.org", 1, undef, $options) or die;
sub mysub {
my ($res) = @_;
foreach my $hashref (@$res) {
my $url = $hashref->{'url'};
my $title = $hashref->{'title'};
print "$title: $url\n";
}
} my @links = $bot->linksearch("slashdot.org", 1) or die;
foreach my $hashref (@links) {
my $link = $hashref->{'link'};
my $page = $hashref->{'page'};
print "$page: $link\n";
}
what_links_here()
This method now uses keys 'title' and 'redirect'. It also returns only links (incl. redirects). Transclusions are handled by list_transclusions(). This reduces confusion in cases where the programmer wishes to use a callback hook to do incremental processing. Without this change, accessing data would have been done with two different sets of keys. Any call to this method needs to be evaluated as to whether it needs to be replaced with or supplemented by a call to list_transclusions(). Also, key names must be updated. my @links = $bot->what_links_here("Meta:Sandbox", undef, 1, {hook=>\&mysub});
sub mysub{
my ($res) = @_;
foreach my $hash (@$res) {
my $title = $hash->{'title'};
my $is_redir = $hash->{'redirect'};
print "Redirect: $title\n" if $is_redir;
print "Page: $title\n" unless $is_redir;
}
} # dunno
Other changes
...