Think about Loose Coupling PerlMonks

### How To: Make An Iterator

by Limbic~Region (Chancellor)
 on Apr 25, 2005 at 17:20 UTC Need Help??

```sub gen_iterator {
my @initial_info = @_;
...
return \$current_state = \$next_state;
};
}
```
```my \$next = gen_iterator( 42 );
while ( my \$item = \$next->() ) {
print "\$item\n";
}
```
```my @DNA = qw/A C T G/;
my \$seq = gen_permutate(14, @DNA);
...
return join '', map { \$list[ \$_ ] } @curr;
};
}
```
```my \$start = \$ARGV[0] || 999999;
my \$next_id = gen_id( \$start );
...
\$tot += \$_ for split //, \$chk;
return \$tot % 10 == 0 ? 1 : 0;
}
```
```my \$next_file = rotate( qw/FileA FileB FileC/ );
print \$next_file->(), "\n" for 1 .. 10;
...
return \$list[ \$index ];
};
}
```
```while ( my \$pass = \$next_pw->() ) {
if ( unlock( \$pass ) ) {
...
};
}
sub unlock { \$_[0] eq 'john' }
```
```my @list = map { my @lines = <\$_>; \@lines } @f_handles;
# becomes
...
return \@lines;
};
}
```
```sub infinite_evens {
my \$curr = shift;
...
}
my \$next_even = infinite_evens( 41 );
print \$next_even->(), "\n" while 1;
```

Create A New User
Node Status?
node history
Node Type: perltutorial [id://451278]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-11-23 18:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (337 votes). Check out past polls.

Notices?