{
my $init = 'aa';
my $start = 'a';
my $len = length $init;
sub next_term {
my $ret = $init;
my $p = $len - 1;
while (1) {
substr($init, $p, 1) =~ tr/acgnt/cgnta/;
last if substr($init, $p--, 1) ne $start;
$init = "$start$init", $len++, last if $p < 0;
}
return $ret;
}
}
####
package Tie::ScalarIter;
sub TIESCALAR {
my ($class, $init, $start) = @_;
bless [ $init, $start, length($init) ], $class;
}
sub FETCH {
my $s = shift;
my $ret = $s->[0];
my $p = $s->[2] - 1;
while (1) {
substr($s->[0], $p, 1) =~ tr/acgnt/cgnta/;
last if substr($s->[0], $p--, 1) ne $s->[1];
$s->[0] = "$s->[1]$s->[0]", $s->[2]++, last if $p < 0;
}
return $ret;
}
sub STORE {
my $s = shift;
my $rep = shift;
if (ref $rep) { @$s = (@$rep, length $rep->[0]) }
else { (@$s[0,2] = ($rep, length $rep) }
}
1;
####
# functional
my @strings;
while (defined (my $next = next_term())) {
last if length($next) > 4;
push @strings, $next;
}
# tied scalar
use Tie::ScalarIter;
tie my($iter), 'Tie::ScalarIter', 'aa', 'a';
my @strings;
while (defined (my $next = $iter)) {
last if length($next) > 4;
push @strings, $next;
}