User:Polbot/code/Function 1

use strict;
use Perlwikipedia;

my $pw=Perlwikipedia->new();
#$pw->{debug} = 1;
$pw->{mech}->agent('Firefox/2.0.0.6');
$pw->set_wiki('en.wikisource.org', 'w');
print "Logging in as Polbot\n";
my $login_status=$pw->login('Polbot','[password]');
die "I can't log in." unless ($login_status == 0);

print "Reading textfile.\n\n";
open(infile, "<needsrd.txt");

while (my $line = <infile>) {
	chop $line;
	$line =~ m/^\[\[(.*)\]\] to \[\[(.*)\]\]$/;
	my $rdat = $1;
	my $rdto = $2;
	print "\n[[$rdat]] -> [[$rdto]]\n";

	my $wiki = $pw->get_text($rdat);
	
	if ($wiki !~ m/\S/) {
		my $wiki = $pw->get_text($rdto);
		if ($wiki =~ m/\S/) {
			print " Creating redirect.\n";
			$pw->edit($rdat, "#Redirect [[$rdto]]", "#Redirect [[$rdto]]");
			sleep 1;
		} else {
			print " Oops, I can't find the new location.\n";
		}
	} else {
		print " Oops, file exists already.\n";
	}
}
close(infile);

print "Done.\n";