Acme-Cow

 view release on metacpan or  search on metacpan

Cow.pm  view on Meta::CPAN

package Acme::Cow;

use strict;

$Acme::Cow::VERSION = '0.2.1';

# Preloaded methods go here.

# Autoload methods go after =cut, and are processed by the autosplit program.

# Below is stub documentation for your module. You better edit it!

=head1 NAME

Acme::Cow - Talking barnyard animals (or ASCII art in general)

=head1 SYNOPSIS

  use Acme::Cow;

  $cow = new Acme::Cow;
  $cow->say("Moo!");
  $cow->print();

  $sheep = new Acme::Cow::Sheep;	# Derived from Acme::Cow
  $sheep->wrap(20);
  $sheep->think();
  $sheep->text("Yeah, but you're taking the universe out of context.");
  $sheep->print(\*STDERR);

  $duck = new Acme::Cow(File => "duck.cow");
  $duck->fill(0);
  $duck->say(`figlet quack`);
  $duck->print($socket);


=head1 DESCRIPTION

Acme::Cow is the logical evolution of the old cowsay program.  Cows
are derived from a base class (Acme::Cow) or from external files.

Cows can be made to say or think many things, optionally filling
and justifying their text out to a given margin,

Cows are nothing without the ability to print them, or sling them
as strings, or what not.

=cut

use Acme::Cow::TextBalloon;
use IO::File;
use Text::Template;

$Acme::Cow::default_cow = <<'EOC';
{$balloon}
        {$tl}   ^__^
         {$tl}  ({$el}{$er})\_______
            (__)\       )\/\
             {$U} ||----w |
                ||     ||
EOC

=pod

=head1 METHODS

=head2 new

=over 4

Cow.pm  view on Meta::CPAN


sub print 
{
    my $self = shift;
    my $fh = shift || \*STDOUT;
    print $fh $self->as_string();
}

=pod

=head2 fill

Inform the cow to fill and adjust (or not) the text inside its balloon.
By default, text inside the balloon is filled and adjusted.

=over 4

=item Parameters

(optional) A scalar; true if you want it to fill and adjust, false
otherwise.

=item Returns

The current fill/adjust state, or the new one after setting.

=back

=cut

sub fill 
{
    my $self = shift;
    if (@_) {
	$self->{'fill'} = $_[0];
    }
    return $self->{'fill'};

}

=pod

=head2 as_string

Render the cow as a string.

=over 4

=item Parameters

(optional) A scalar that can be interpreted as a C<STRING> type
for C<Text::Template>.

=item Returns

An ASCII rendering of your cow.

=item Notes

If you're using an external file for a cow template, any difficulties
in processing the file will occur in this method.

Every time this method is called, the result is recalculated; there
is no caching of results.

=back

=cut

sub as_string 
{
    my $self = shift;
    my $tmpl = shift;
    if (not $tmpl) {
	if (defined $self->{'File'}) {
	    $tmpl = _slurp_file($self->{'File'});
	} else {
	    $tmpl = $Acme::Cow::default_cow;
	}
    }
    my $b = $self->_create_balloon();
    my $template = new Text::Template(TYPE => 'STRING', SOURCE => $tmpl);
    chomp($Acme::Cow::_private::balloon = $b->as_string());
    $Acme::Cow::_private::el = $self->{'el'};
    $Acme::Cow::_private::er = $self->{'er'};
    $Acme::Cow::_private::U = $self->{'U'};
    $Acme::Cow::_private::tl = ($self->{'mode'} eq 'think') ? 'o' : '\\';
    $Acme::Cow::_private::tr = ($self->{'mode'} eq 'think') ? 'o' : '/';
    my $text = $template->fill_in(PACKAGE => 'Acme::Cow::_private');
    return $text;
}

sub _create_balloon
{
    my $self = shift;
    my $b = new Acme::Cow::TextBalloon;
    for my $i (qw(fill text over mode wrap)) {
	$b->{$i} = $self->{$i};
    }
    return $b;
}

sub _slurp_file
{
    my $filename = shift;
    my $fh = new IO::File($filename);
    local $/ = undef;
    my $text = $fh->getline();
    return $text;
}

1;
__END__

=pod

=head1 WRITING YOUR OWN COW FILES

First, get comfortable with C<Text::Template> and its capabilities.

{$balloon} is the text balloon; it should be on a line by itself,



( run in 0.786 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )