Loop - Smart, Simple, Recursive Iterators for Perl programming.


Loop documentation  | view source Contained in the Loop distribution.

Index


NAME

Top

Loop - Smart, Simple, Recursive Iterators for Perl programming.

SYNOPSIS

Top

  use Loop;

  Loop::Array @array, sub
	{
	my ($index,$value)=@_;
	print "at index '$index', value='$value'\n";
	}

ABSTRACT

Top

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.

DESCRIPTION

Top

Loop on an Array

  # 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

  # 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 on a File

  # 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";
		}
	};




EXPORT

none

SEE ALSO

Top

AUTHOR

Top

Greg London, http://www.greglondon.com

COPYRIGHT AND LICENSE

Top


Loop documentation  | view source Contained in the Loop distribution.