Acme-POE-Acronym-Generator

 view release on metacpan or  search on metacpan

lib/Acme/POE/Acronym/Generator.pm  view on Meta::CPAN

package Acme::POE::Acronym::Generator;
{
  $Acme::POE::Acronym::Generator::VERSION = '1.20';
}

#ABSTRACT: Generate random POE acronyms.

use strict;
use warnings;
use Math::Random;

sub new {
  my $package = shift;
  my %opts = @_;
  $opts{lc $_} = delete $opts{$_} for keys %opts;
  $opts{dict} = '/usr/share/dict/words' unless $opts{dict};
  $opts{key} =~ s/[^A-Za-z]//g if $opts{key};
  $opts{key} = lc $opts{key} if $opts{key};
  my $self = bless \%opts, $package;

  $self->{poe} = [ $opts{key}? split( //, $opts{key} ) : qw(p o e) ];
  my $key = join '', @{$self->{poe}};

  if ( $opts{wordlist} and ref $opts{wordlist} eq 'ARRAY' ) {
	for ( @{ $opts{wordlist} } ) {
	   chomp;
	   next unless /^[$key]\w+$/;
	   push @{ $self->{words}->{ substr($_,0,1) } }, $_;
	}
	return $self;
  }
  if ( -e $opts{dict} ) {
	open FH, "< $self->{dict}" or die "$!\n";
	while (<FH>) {
	   chomp;
	   next unless /^[$key]\w+$/i;
	   # next unless /^[poe]\w+$/;
	   push @{ $self->{words}->{ substr($_,0,1) } }, $_;
	}
	close FH;
  }
  else {
	$self->{words} = 
	{
	   'p' => [ qw(pickle purple parallel pointed pikey) ],
	   'o' => [ qw(orange oval ostrich olive) ],
	   'e' => [ qw(event enema evening elephant) ],
	}
  }
  return $self;
}

sub generate {
  my $self = shift;
  my $words = $self->{words};
  my @poe;
  push @poe, 
	ucfirst( $words->{$_}->[ scalar random_uniform_integer(1,0,scalar @{ $words->{$_} }-1 ) ] ) for ( @{$self->{poe}} );
  return wantarray ? @poe : join( ' ', @poe );
}

1;

__END__

=pod

=head1 NAME

Acme::POE::Acronym::Generator - Generate random POE acronyms.

=head1 VERSION

version 1.20

=head1 SYNOPSIS

  use strict;
  use warnings;



( run in 2.527 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )