#! /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; }

Lorem ipsum tag1example1 sit amet, consectetur adipiscing elit. Vestibulum bibendum mi in ipsum tag2example1 id sagittis dolor ultrices. Maecenas vitae nunc diam, quis gravida augue. Integer...

Lorem ipsum tag1example2 sit amet, consectetur adipiscing elit. Vestibulum bibendum mi in ipsum tag2example2 id sagittis dolor ultrices. Maecenas vitae nunc diam, quis gravida augue. Integer...

Lorem ipsum tag1example1 sit amet, consectetur adipiscing elit. Vestibulum bibendum mi in ipsum tag2example3 id sagittis dolor ultrices. Maecenas vitae nunc diam, quis gravida augue. Integer...

Lorem ipsum tag1example2 sit amet, consectetur adipiscing elit. Vestibulum bibendum mi in ipsum tag2example1 id sagittis dolor ultrices. Maecenas vitae nunc diam, quis gravida augue. Integer...

--------------------------

If this is homework and you haven't covered oo yet you may have some explaining to do. :-)

The best? Unlikely. Efficient? Depends. I found it easy to write and, in imo, it reads well so I'll find it easier to maintain. It would also be reasonably easy to extend.

update: In some quarters passing arguments around as hashrefs is considered bad for your health - so be prepared. :-)


In reply to Re: Text Tagging by wfsp
in thread Text Tagging by anlamarama

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.