Closure::Loop 0.0.2
Sponsored Links
Closure::Loop 0.0.2 Ranking & Summary
File size:
0.006 MB
Platform:
Any Platform
License:
Perl Artistic License
Price:
Downloads:
8665
Date added:
2007-01-18
Publisher:
Andy Armstrong
Publisher URL:
http://www.hexten.net/mod
Closure::Loop 0.0.2 description
Closure::Loop is a Perl module with redo, last, next for closure based loops.
SYNOPSIS
package MyIterator;
use Closure::Loop; # mixin
sub new {
my $class = shift;
return bless { }, $class;
}
sub forAll {
my $self = shift;
my $cb = pop || die "No callback";
for my $i (@_) {
eval {
$self->yield($cb, $i);
};
last if $self->is_last;
die $@ if $@;
}
}
package main;
my $iter = MyIterator->new();
my @in = ( 1, 2, 3 );
my @out = ( );
$iter->forAll(@in, sub {
my $i = shift;
$iter->next if $i == 2; # skip value
push @out, $i;
});
# @out is ( 1, 3 )
An idea that Perl programmers can usefully borrow from Ruby is the concept of synthesizing new looping constructs by passing a block of code to a function that repeatedly calls it with successive values in a sequence.
In Ruby this looks like this:
def count_to_ten
i = 1
while i <= 10
yield i
i = i + 1
end
end
count_to_ten do |i|
puts i
end
In Perl the same thing looks like this:
sub count_to_ten {
my $block = shift;
my $i = 1;
while ($i <= 10) {
$block->($i);
$i++;
}
}
count_to_ten(sub {
my $i = shift;
print "$in";
});
That example is deliberately trivial. In practice this technique can be used to implement a loop like construct that, for example, walks the nodes in a binary tree - something that isnt easy with a normal loop.
The body of the loop is actually a closure so it has complete access to the lexical scope in which it is defined. In short it works just like the body of a loop for our purposes - with one exception.
For normal Perl loops its possible to use the last, next and redo statements to modify iteration of the loop. That wont work as expected here. If we use them in our pseudo loop well get a warning (because were actually trying to jump out of a subroutine using them) and theyll end up affecting the first enclosing loop or block they find - which may impact unpredictably on the iterator function, particularly if it doesnt actually contain a loop.
This module makes it easy to implement modules that expose iterator methods and which provide an interface for controlling the iteration of the loop which resembles an object oriented version of Perls normal loop control statements.
SYNOPSIS
package MyIterator;
use Closure::Loop; # mixin
sub new {
my $class = shift;
return bless { }, $class;
}
sub forAll {
my $self = shift;
my $cb = pop || die "No callback";
for my $i (@_) {
eval {
$self->yield($cb, $i);
};
last if $self->is_last;
die $@ if $@;
}
}
package main;
my $iter = MyIterator->new();
my @in = ( 1, 2, 3 );
my @out = ( );
$iter->forAll(@in, sub {
my $i = shift;
$iter->next if $i == 2; # skip value
push @out, $i;
});
# @out is ( 1, 3 )
An idea that Perl programmers can usefully borrow from Ruby is the concept of synthesizing new looping constructs by passing a block of code to a function that repeatedly calls it with successive values in a sequence.
In Ruby this looks like this:
def count_to_ten
i = 1
while i <= 10
yield i
i = i + 1
end
end
count_to_ten do |i|
puts i
end
In Perl the same thing looks like this:
sub count_to_ten {
my $block = shift;
my $i = 1;
while ($i <= 10) {
$block->($i);
$i++;
}
}
count_to_ten(sub {
my $i = shift;
print "$in";
});
That example is deliberately trivial. In practice this technique can be used to implement a loop like construct that, for example, walks the nodes in a binary tree - something that isnt easy with a normal loop.
The body of the loop is actually a closure so it has complete access to the lexical scope in which it is defined. In short it works just like the body of a loop for our purposes - with one exception.
For normal Perl loops its possible to use the last, next and redo statements to modify iteration of the loop. That wont work as expected here. If we use them in our pseudo loop well get a warning (because were actually trying to jump out of a subroutine using them) and theyll end up affecting the first enclosing loop or block they find - which may impact unpredictably on the iterator function, particularly if it doesnt actually contain a loop.
This module makes it easy to implement modules that expose iterator methods and which provide an interface for controlling the iteration of the loop which resembles an object oriented version of Perls normal loop control statements.
Closure::Loop 0.0.2 Screenshot
Sponsored Links
Closure::Loop 0.0.2 Keywords
Loop 0.0.2
ClosureLoop
Perl module
last
Perl
redo
module
0.0.2
closure
Closure::Loop 0.0.2
Libraries
Programming
Bookmark Closure::Loop 0.0.2
Closure::Loop 0.0.2 Copyright
WareSeeker periodically updates pricing and software information of Closure::Loop 0.0.2 full version from the publisher, so some information may be slightly out-of-date. You should confirm all information before relying on it. Software piracy is theft, Using crack, password, serial numbers, registration codes, key generators is illegal and prevent future development of Closure::Loop 0.0.2 Edition. Download links are directly from our mirror sites or publisher sites, torrent files or links from rapidshare.com, yousendit.com or megaupload.com are not allowed
Closure::Loop 0.0.2 Related Software
LOOP for Firefox is an extension that provides a revolutionary way to document web pages while browsing the Internet.
Animation fade-loop is a plugin for GIMP that can convert an animation to make it look better when played in a continuous loop.
Danga::Socket is an event loop and event-driven async socket base class.
libloop is a small, simple library for manipulating loopback block devices (/dev/loop).
POE::Loop::Event is a bridge that supports Gtks event loop from POE.
My Software
You have not saved any software. Click "Save" next to each software to save it to your software basket
Related Information
Sponsored Links
TOP POPULAR DOWNLOAD