Loop - Smart, Simple, Recursive Iterators for Perl programming.
use Loop; Loop::Array @array, sub { my ($index,$value)=@_; print "at index '$index', value='$value'\n"; }
This module is intended to implement simple iterators on perl variables with little code required of the programmer using them.
Some additional advantages over standard perl iterators:
Array iterators give access to the current index within the array. Hash iterators can be nested upon the same hash without conflicts. File iterators allow simple file munging in a few lines of code.
# loop on an array, at index 3, change the value in the array to "three" my @array = qw (alpha bravo charlie delta echo); Loop::Array @array, sub { my($index,$val)=@_; if($index == 3) { # modify the element in the original array # note that when you want to change the original array, # you must assign to the parameter array @_ $_[1] = 'three'; } }
# loop on a hash, perform nested iteration on the same hash. my %hash = ( blue => 'moon', green => 'egg', red => 'baron', ); Loop::Hash (%hash, sub { my($key1,$val1)=@_; print "checking key1 $key1, val1 $val1 for collisions \n"; Loop::Hash (%hash, sub { my($key2,$val2)=@_; print "\tchecking key2 $key2, val2 $val2 for collisions \n"; print "\t $val2 is not $key1\n" unless($key1 eq $key2); return; }); });
# loop through a file, read it line by line, and grep for a string. Loop::File "tfile.pl", sub { my($linenum,$linestr)=@_; if($linestr =~ /search/) { print "found at line $linenum: $linestr"; } };
none
Greg London, http://www.greglondon.com
Copyright 2003 by Greg London, All Rights Reserved
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Loop, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Loop
CPAN shell
perl -MCPAN -e shell install Loop
For more information on module installation, please visit the detailed CPAN module installation guide.