#! /usr/bin/perl use strict; use warnings; my $t = Tags->new( { txt_file => q{text.txt}, tag_files => [qw{tag1.txt tag2.txt}], } ); my $count = 3; for (0..$count){ print $t->output; } package Tags; sub new { my $class = shift; my $args = shift; my $self = {}; die q{no txt file} unless exists $args->{txt_file}; die q{no tag files} unless exists $args->{tag_files}; my $txt_file = $args->{txt_file}; open my $fh, q{<}, $txt_file or die qq{cant open *$txt_file* to read: $!}; $self->{txt} = do{local $\;<$fh>}; my @files = @{$args->{tag_files}}; die q{no tag files} unless @files; for my $file (@files){ open my $fh, q{<}, $file or die qq{cant open *$file* to read: $!}; my @lines = <$fh>; chomp @lines; my ($tag_name) = $file =~ /([^.]+)\./; $self->{$tag_name} = \@lines; } bless $self, $class; return $self; } sub output { my $self = shift; my $txt = $self->{txt}; $txt =~ s/{{([^}]+)}}/$self->next({tag => $1})/eg; return $txt; } sub next { my $self = shift; my $args = shift; my $tag = $args->{tag}; die q{bad tag} unless exists $self->{$tag}; my $tag_txt = shift @{$self->{$tag}}; push @{$self->{$tag}}, $tag_txt; return $tag_txt; }