Coder Social home page Coder Social logo

io-lambda's Introduction

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.

io-lambda's People

Contributors

dk avatar manwar avatar pmorch avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

pmorch manwar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.