#!/usr/bin/perl use strict; use warnings; # make our iterator (a subroutine which holds its own state) my $ones = seq_gen(1); while (1) { # call iterator and print result my $next = $ones->(); print "$next\n"; sleep 1; } sub seq_gen { # generate and return a subroutine that captures its entry values and # gives you the next member each time it is called my $seq = shift; my $string = ''; return sub { $string .= $seq; return $string; } }