<?xml version="1.0" encoding="windows-1252"?>
<node id="441274" title="Confessions of a virtual monk" created="2005-03-21 14:13:10" updated="2005-08-12 03:07:39">
<type id="1042">
CUFP</type>
<author id="439923">
polettix</author>
<data>
<field name="doctext">
Powerful Monks,

   please forgive me because I've committed a sin. After few days inside this community, I've had the arrogance to elevate myself to the level of gods and create a Monk from /dev/null...

&lt;readmore&gt;
&lt;p&gt;
It actually taught me to use WWW::Mechanize, which I find quite useful! Here's the code for the virtual monk:

&lt;code&gt;
#!/usr/bin/perl

use strict;
use warnings;
use WWW::Mechanize;
use HTTP::Response;

my ($user, $passwd) = ("anotherTime", "no_abuse_please");
my $homepage = 'http://www.perlmonks.org/';

print STDERR "getting homepage [$homepage]\n";
my $mech = WWW::Mechanize-&gt;new( agent =&gt; 'Virtual Monk 0.1' );
$mech-&gt;get($homepage)-&gt;is_success() || die "unable to get $homepage, stopped";

print STDERR "logging in\n";
$mech-&gt;submit_form(
	form_name =&gt; 'login',
	fields =&gt; { user =&gt; $user, passwd =&gt; $passwd },
	button =&gt; 'login'
)-&gt;is_success() || die "unable to log in $homepage, stopped";
# Ensure we've logged in successfully
my $logout = $mech-&gt;find_link( text =&gt; "log $user out" );
die "did not log in, stopped" unless $logout;

my $votes = 0;
for (my $maxcount = 0; $maxcount &lt; 100; ++$maxcount) {
	print STDERR "going in my home node\n";
	$mech-&gt;follow_link( text =&gt; $user )-&gt;is_success()
		|| die "could not get into my home node, stopped";

	print STDERR "assessing if I've spare votes\n";
	last unless $mech-&gt;response()-&gt;content() =~ /You have &lt;b&gt;([0-9]+)&lt;\/b&gt; votes? left today\./;
	last unless $1;
	
	# Find a page where I can vote. Index indicates the index of the voting form
	my $index = 0;
	for (my $maxcount2 = 0; $maxcount2 &lt; 100 &amp;&amp; ! $index; ++$maxcount2) {
		print STDERR "getting a random page\n";
		$mech-&gt;follow_link( text =&gt; 'Random Node' )-&gt;is_success()
			|| die "could not get a random node, stopped";
		
		print STDERR "verifying I can vote anywhere in it\n";
		my $count = 0;
		foreach my $form ($mech-&gt;forms()) {
			++$count; # Form numbers start from 1
			if (defined($form-&gt;find_input('sexisgreat', 'submit'))) {
				$index = $count; 
				last;
			}
		}
	}
	
	if ($index) { # A voting form is available
		print STDERR "casting a vote\n";
		$mech-&gt;form_number($index);
		$mech-&gt;set_visible( [ radio =&gt; "1" ] );
		$mech-&gt;click('sexisgreat');
		++$votes;
	}
}

print STDERR "$votes vote" . ($votes != 1 ? "s" : "") . " cast\n";

# Log out
print STDERR "logging out\n";
$mech-&gt;get($logout-&gt;url()) || die "unable to log out, stopped";
&lt;/code&gt;

&lt;/readmore&gt;

The virtual monk in the configuration is a registered account... forgive me for this as well. Being an acolyte, I think it should be expected some - ehr - inclination to break rules..
&lt;p&gt;The virtual monk isn't running in a cron job, tough. No, it's not running with my account as well :) I'm only wondering how much time it would require him to become saint - I'll evaluate this and post here if some god doesn't kick me out!
&lt;P&gt;Flavio.

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-439923"&gt;
--
&lt;i&gt;Don't fool yourself.&lt;/i&gt;
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
