Skip to content
non-blocking I/O as lambda calculus
Perl Perl6 Other
Find file
New pull request
Latest commit 0b61298 @dk v1.25
Failed to load latest commit information.


IO::Lambda - nonblocking IO in functional style

This module is another attempt to fight the horrors of non-blocking I/O
programming. The simplicity of the sequential programming is only available
when one employs threads, coroutines, or coprocesses. Otherwise state machines
are to be built, often quite complex, which fact doesn't help the clarity of
the code. This module uses closures to achieve clarity of sequential
programming with single-process, single-thread, non-blocking I/O.

The approach implemented in this module is strongly based on closures.
It emulates the blocking style, so the collection of states is local, and can
be collected under single subroutine. For example, a dumbed down HTTP protocol
can be described like in the following scheme:

	writable {
		print $fh "GET /\r\n\r\n" or die;
		readable {
			sysread $fh or die;

IO::Lambda features syntax where one can indeed use the lambda syntax.
Below is a full-functioning code:

	use strict;
	use IO::Lambda qw(:lambda);
	use IO::Socket::INET;
	my $q = lambda {
		my ( $socket, $url) = @_;
		context $socket;
		writable {
			print $socket "GET $url HTTP/1.0\r\n\r\n";
			my $buf = '';
			readable {
				return $buf unless 
					sysread( $socket, $buf, 1024, length($buf));
	print $q-> wait( 
		IO::Socket::INET-> new( 
			PeerAddr => '', 
			PeerPort => 80 

For more examples, see eg/ directory.
Something went wrong with that request. Please try again.