> It goes HEADING|DATE|NEWS, so, I want to use Template Toolkit to pass
> this data to my template file. Here is what I have so far, but, I can't
> figure out how to separate the data by the |'s and update my news.

Ideally, you'd do this in your script, not in the template, so it's really
more of a perl question.

> # use strict;

tsk, tsk.   :)

> sub get_news {
>     open(INF,"data/news.db");
>     @news = <INF>;
>     close(INF);
>     return \@news;
> }

Try this:

sub get_news {
    #always good to check your return codes
    open(INF, "data/news.db") or die "Can't open news.db: $!";
    my @all_news;
    while (my $line = <INF>){
        chomp($line); #get rid of trailing \n that you probably don't want
        my %news;
        ($news{Heading}, $news{Date}, $news{News}) = split(/\|/,$line);
        push @all_news, \%news;
    }
    return \@all_news;
}

This says:
for each line of News.db
-remove the trailing newline (or whatever it is windows uses)
-create a new hash
-split up the line at pipe characters, and assign to the given elements of
%news
-push a reference to %news onto an array.  (note that each time through
the loop %news is actually a different variable)  [More newbie notes:  An
array stores scalars.  Thus, an array can't store a hash.  A reference,
however, is a scalar, so you can store a reference to a hash.  (or
anything else).  And we often do.]
-return a reference to the array.

Thus your templates will be like:

 [% FOREACH line = array %]
     [% line.Heading %]<p>
     [% line.Date %] <hr>
     [% line.News %]
 [% END %]

> output into a nicely formatted table? Also, Perl is the first language
> I�ve ever tried to learn after HTML, I would enjoy book suggestions:

Perl is a great choice, it's the breakfast of champions :)

> complicated for me, so, I was told to pick up Learning Perl next, any

I'd agree, Learning Perl is the way to go.  Most everything else assumes
some programming experience.

I also recommend perlmonks.org.  Vast amounts of Perl lore reside there.

Hope this helps.





Reply via email to