Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How can I create a "good" news script

by bladx (Chaplain)
on Jan 01, 2002 at 00:53 UTC ( #135429=perlquestion: print w/ replies, xml ) Need Help??
bladx has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone.

I am back at it again, trying to get this news script project (that I have been working on for the past week) to work. I know that the easiest way to create a simple news script (and what I mean by a "news script" is a Perl-based CGI script that displays items from a database key and value hash.) I would like to be able to somewhere in my code be able to split elements of each line using a deliminator of | to seperate items such as the date of creation of article, the author, the title, the news, the avatar to go along with it, etc. But my problem is, I can't figure out (even after reading through the DB_File manpage,) how to do this. Is there a much better way to do this rather than to split a value of a key? (i.e. I am thinking of doing it like this: I have a key, then a value of something like: title|date|author|news and I would like to be able to split each individual key up so that I can use these variables or refrences in the HTML template later. That way I can have like a box of information using each variable from each value.

This is what I was planning on doing to create this news script, but is there a way I am missing, on how to do this sort of thing, using Perl and DB_File and HTML::Template? I hope that this time I have explained more about what I am doing than my past latest posts. For further clarification, the current .pl file I am doing this in is here:
#!/usr/bin/perl -w use strict; use HTML::Template; use CGI::Carp qw(fatalsToBrowser); use DB_File; my @fields = ("title","date","author","news"); my $template = HTML::Template->new(filename => 'news.tmpl'); my $filename="database.db"; tie(my %hash, "DB_File", $filename) or die "Can't open $filename: $!"; my @loop_data = map { { KEY => $_, VALUE => $hash{$_} } } keys %hash; untie %hash; $template->param(SHOW_ALL_LOOP => \@loop_data); print $template->output;


Is there a way I can split each value in this script somewhere? If there is another way that is better than this way, please let me know! I don't want to be using something that is either old, or something that does not work at all. Thanks for any help.

Andy Summers

Edit ar0n -- title fix

Comment on How can I create a "good" news script
Select or Download Code
(Ovid) Re: How can I create a "good" news script
by Ovid (Cardinal) on Jan 01, 2002 at 01:26 UTC

    Instead of trying to split on a character that might accidentally show up in your code, why don't you just serialize a data structure?

    use strict; use warnings; use Storable qw/ freeze thaw /; use Data::Dumper; my %foo = ( one => [ qw/ a b c / ], two => [ qw/ 1 2 3 / ] ); my $serialized = freeze \%foo; my %clone = %{ thaw $serialized }; print Dumper \%clone;

    It's much cleaner that way and you can store it in your database quite nicely (rethinking the database design is better, though).

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Or possibly use a DBM interface that handles serialization for you, like MLDBM. If you are worried about file locking and concurrancy, you can also use MLDBM::Sync. MLDBM has saved me a lot of work.


      TGI says moo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://135429]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-10-01 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (29 votes), past polls