#!/usr/bin/perl -w use strict; my $cycle = initToggle( 3 ); for (1..24) { print "$_ " . &$cycle . "\n"; } sub initToggle { my $limit = shift; die "initToggle() requires a non-zero integer\n" if ($limit == 0 or int($limit) != $limit); my $count = -1; my $bit = $limit >0 ? 0 : 1; $limit = abs($limit); return sub { $bit ^=1 if (++$count % $limit == 0); return $bit; } }