Skip to content

dk/IO-Lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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;
			read_again;
		}
	}

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));
				again;
			}
		}
	};
	print $q-> wait( 
		IO::Socket::INET-> new( 
			PeerAddr => 'www.perl.com', 
			PeerPort => 80 
		),
		'/index.html'
	);

For more examples, see eg/ directory.

About

non-blocking I/O as lambda calculus

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages