reCAPTCHA example in Perl

This example Perl CGI program demonstrates how to send a CAPTCHA to Google and process the response, without using a special module such as Captcha::reCAPTCHA. The script requires CGI or a similar web module, LWP::UserAgent to send the request to Google, and JSON::Parse to parse the response from Google. It should be simple to substitute other modules for these.

You need to supply your own value for $secret, which is obtained from Google.

use warnings;
use strict;
use CGI;
use LWP::UserAgent;
use JSON::Parse 'parse_json';

my $secret = 'ThisIsNotARealOne';
my $gurl = '';

my $cgi = CGI->new ();

if (! check_captcha ($cgi)) {
    # OK

sub check_captcha
    my ($icgi) = @_;
    my $ua = LWP::UserAgent->new ();
    my $response = $cgi->param ('g-recaptcha-response');
    my $remoteip = $ENV{REMOTE_ADDR};
    my $greply = $ua->post (
            remoteip => $remoteip,
            response => $response,
            secret => $secret,
    if ($greply->is_success ()) {
        my $json = $greply->decoded_content ();
        my $out = parse_json ($json);
        if ($out->{success}) {
            return undef;
        return $json;
    return "Connection to reCAPTCHA failed";


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer