Using reCAPTCHA with Perl

Important: Version 1.0 of the reCAPTCHA API is no longer supported, please upgrade to Version 2.0. Learn more

The reCAPTCHA Perl Module provides a simple way to place a CAPTCHA on your website, helping you stop bots from abusing it. The module wraps the reCAPTCHA API.

To use reCAPTCHA with Perl, you can download the reCAPTCHA Perl Module (contributed by Andy Armstrong). You will need to install this module on your machine (web server). The module depends on the modules LWP::UserAgent and HTML::Tiny, both of which will also need to be installed. Here are some basic instructions on installing Perl modules.

Quick Start

After you've signed up for your API keys and downloaded the reCAPTCHA Perl module, below are basic instructions for installing reCAPTCHA on your site.

Client Side (How to make the CAPTCHA image show up)

If you want to use the Perl module to display the reCAPTCHA widget, you'll need to insert this line near the top of the file with the form element where the reCAPTCHA widget will be displayed:

    use Captcha::reCAPTCHA;

Then, you need to create an instance of reCAPTCHA:

    my $c = Captcha::reCAPTCHA->new;

Finally, to display the reCAPTCHA widget, you must place the following line inside the <form> tag:

    print $c->get_html("your_public_key");

So, your code may look something like this:

    use Captcha::reCAPTCHA;

    my $c = Captcha::reCAPTCHA->new;

    print <<EOT;
    <html>
      <body>
        <form action="" method="post">
    EOT

    print $c->get_html("your_public_key");

    print <<EOT;
        <input type="submit" value="submit" />
        </form>
      </body>
    </html>
    EOT

Don't forget to replace your_public_key with the value of your API key.

Server Side (How to test if the user entered the right answer)

Below is a skeleton of how to verify the reCAPTCHA answer:

    use Captcha::reCAPTCHA;

    my $c = Captcha::reCAPTCHA->new;

    my $challenge = param 'recaptcha_challenge_field';
    my $response = param 'recaptcha_response_field';

    # Verify submission
    my $result = $c->check_answer(
        "your_private_key", $ENV{'REMOTE_ADDR'},
        $challenge, $response
    );

    if ( $result->{is_valid} ) {
        print "Yes!";
    }
    else {
        # Error
        print "No";
    }

Further Reading

  • Customizing Look and Feel
  • Tips and Guidelines
  • Troubleshooting