#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub create_iter { my $start = shift || 1; my $func = shift || sub { $_[0] += 1 }; return sub { my $t = $start; $func->($start); return $t } } my $iter = create_iter(17, sub { $_[0] += 2 }); while (my $i = $iter->()) { last if $i > 25; print $i . "\n"; } my $iter2 = create_iter(1, sub { $_[0] *= 2 }); while (my $i = $iter2->()) { last if $i > 256; print $i . "\n"; }