package CodeGenerator; use strict; use warnings; sub new { my $class = shift; my $self = bless {}, $class; $self->{string_size} = shift; $self->{digits_used} = [ @_ ]; $self->{seen} = {}; # Not necessary, but helpful to # those reading the code. return $self; } sub generate { my $self = shift; my $rand_string; do{ $rand_string = ''; $rand_string .= $self->{digits_used}[ rand @{$self->{digits_used}} ] for 1 .. $self->{string_size}; } while( $self->{seen}{$rand_string}++ ); return $rand_string; } 1; package main; use strict; use warnings; use autodie; use v5.10; # For "say". # Some criteria. my @digits_used = ( 'a' .. 'z', 3, 4, 6, 7, 9 ); my $string_length = 10; my $need = 6_000_000; my $filename = 'testfile.txt'; my $codegen = CodeGenerator->new( $string_length, @digits_used ); open my $out_fh, '>', $filename; for( my $count = 0; $count < $need; $count++ ) { say $out_fh $codegen->generate(); } close $out_fh;