The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


AnyEvent::SCGI - Event based SCGI server


A simple Hello World SCGI server running on port 22222:

    use AnyEvent::SCGI;
    use HTTP::Headers;

    my $s = scgi_server '', 22222, sub {
        my $handle = shift;
        my $env = shift;
        my $content_ref = shift; # undef if none
        my $fatal_error = shift;
        my $error_string = shift;

        my $headers = HTTP::Headers->new(
            'Status' => '200 OK',
            'Content-Type' => 'text/plain',
            'Connection' => 'close',

        $handle->push_write($headers->as_string . "\r\nHello World!\r\n");


Sets up a SCGI server on the specified port. Can be used with or without Coro. You are responsible for any daemonization and startup code.

The usual AnyEvent callback caveats apply; make sure you don't block or re-enter the event loop in a way that's not supported. This module has been tested for use with Coro, but if you don't want to use that, it's recommended that you return from the callback as quickly as possible.

Using Coro

If you're using Coro, here's the supported calling pattern:

    use Coro;
    use Coro::AnyEvent;
    use AnyEvent;
    use AnyEvent::SCGI;

    my $s = scgi_server $server_name, $port, sub {
        my $handle = shift;
        my $env = shift;
        my $content = shift;

        # handle errors if any

        async { 
            my $stuff = expensive($content);
                $headers->as_string .
                "\r\nHello World!\r\n$stuff"
        # return before running async block


scgi_server $host, $port, $handler_cb

This function creates a TCP socket on the given host and port by calling tcp_server() from AnyEvent::Socket.

Calls $handler_cb when a valid SCGI request has been received. The callback will block other clients until it returns.


The first parameter is the AnyEvent::Handle If the request has a payload, a reference to it is passed in as the $content parameter.

On error, \%env and \$content are undef and the usual $fatal and $error parameters are passed in as subsequent arguments. On "EOF" from the client, fatal is "0" and error is 'EOF'.


Jeremy Stashewsky <>

Kevin Jones <>


Please report any bugs or feature requests to bug-anyevent-scgi at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc AnyEvent::SCGI

You can also look for information at:



Copyright 2009 Jeremy Stashewsky Copyright 2009 Kevin Jones

Copyright 2009 Socialtext Inc., all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.