So, I'm writing with Moose and I decide to try Roles. Why? I'm still not sure, but The Schwartz wrote about it, so it's worth testing. Instead of having a subroutine in the internal module that creates a Text::Template, I created a role for it in the main module.
[after removing irrelevant code]
Before:
package Whatever::Internal;After:
use Carp; use Moose; use Text::Template; # <3 mjd
...
sub build_template { my ( $self, $zone_file ) = @_; my $DOT = q{.};
$zone_file && $self->zone_file($zone_file);
my %vars = ( ip => $self->ip, ... );
my $template = Text::Template->new( SOURCE => $self->zone_file ) or croak "Couldn't construct template: $Text::Template::ERROR\n";
if ( my $result = $template->fill_in( HASH => \%vars ) ) { return $result; } else { croak "Couldn't fill in template: $Text::Template::ERROR\n"; } }
package Whatever;
use Carp; use Moose::Role; use Text::Template; # <3 mjd
...
sub build_template { my ( $self, $zone_file ) = @_;
$zone_file && $self->zone_file($zone_file);
my $template = Text::Template->new( SOURCE => $self->zone_file ) or croak "Couldn't construct template: $Text::Template::ERROR\n";
if ( my $result = $template->fill_in( HASH => $self->template_vars ) ) { return $result; } else { croak "Couldn't fill in template: $Text::Template::ERROR\n"; } }
package Domains::Corp; use Moose; with 'Domains';
...
before 'build_template' => sub { my $self = shift; my $DOT = q{.};
$self->template_vars( { ip => $self->ip, ... } ); };