Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Simple Data storage

by Hissingsid (Sexton)
on Mar 08, 2004 at 11:55 UTC ( [id://334770]=perlquestion: print w/replies, xml ) Need Help??

Hissingsid has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Although I've been dabling in Perl CGI scipting for a couple of years I'm still very much a novice (had to get a Monk pun into my first post)!

I'm looking for the simplest way to store contents and html page design in separate files. Here's what I'm thinking of doing and would appreciate comments on the sanity of the idea. I'm avoiding using any form of database or parsing of data when a web page is sent to the browser.

The contribute script will print a file to a specified directory with the data simply held as variables as they would be declared in a script.

$variable = "lots of text";

The "present to browser" script will decode the URL and covert this into directory and file to include variables.

$inc = #path to file to include

require "$inc";

Imediately all of the variables stored in that external file will be available to me without any fancy parsing.

In effect this will be similar to Blosxom but without the need to pars the external files. I can't figure why someone would convert variables to write out to a file only to have to parse them when they come back in. It seems like a complete waste of resources to me.

I don't need to be able to search or sort records users and editors will just navigate. Directory structure will be stored in a file containing just an array, one per directory. This will be added to as pages are added and reduced as they are deleted.

Am I completely mad or is this a good quick solution. Any comments or advice would be much appreciated.

Many thanks Sid

Replies are listed 'Best First'.
Re: Simple Data storage
by neniro (Priest) on Mar 08, 2004 at 12:17 UTC
    I would use HTML::Template and Storable for this purpose. I've done this for a simple guestbook before and it works quite good. An example:
    #!c:/Perl/bin/Perl.exe ## ## guestbook.cgi - a simple guestbook ## use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); use Html::Template; use Storable; # # CGI Variables # my $q = new CGI; my $name = $q->escapeHTML($q->param('name')) || ''; my $mail = $q->escapeHTML($q->param('mail')) || ''; my $text = $q->escapeHTML($q->param('text')) || ''; my $action = $q->param('action') || ''; my $template = HTML::Template->new(filename => 'guestbook.tmpl', die_o +n_bad_params => 0); my $entries = retrieve 'guestbook.dat'; # # main() # &add_new_entry() if ($action eq 'add'); &flush_entries() if ($action eq 'flush'); print $q->header(); $template->param(entries => $entries); print $template->output(); store $entries, 'guestbook.dat' or die "Can't store guestbook.dat: $!\ +n"; exit(); # # Subroutines # sub add_new_entry { $text =~ s/\n/<br \/>/g; unshift @$entries, { name => $name, mail => $mail, text => $text }; } sub flush_entries { $entries = []; }
      Storable is not a good choice here for a guestbook. Storable is just a serialization mechanism. Storable loads the entire datastructure you serialized into memory every time, so once your guestbook grows to 100 MB, every time you run this program perl is gonna chew up at least 100MB of memory.

      You should use DB_File or MLDBM (uses Storable or DB_File) or similar (you get persistance, ease of use, low memory overhead regardless of how big the file is ).

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

        Yes, thats true. Storable is just for small apps or proof of concepts. In most cases i'd prefer a database like MySQL or if this isn't possible SQLite (no need for a separate db-daemon).
        Hi, Thanks for the replies guys.

        The site I'm working on is a community project on a shared server so I'm resticted in what modules I can use. I've just run PerlDiver to check what is available and none of the modules mentioned here are installed.

        Perl 5.6.1 is the version running on the server. Are there other modules that you can suggest I look for and mug up on or any ides on a solution that does note require any modules other than CGI would be helpful.

        Many Thanks again.

        Best wishes

        Sid

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2024-04-19 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found