in reply to Simple Data storage

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 = []; }

Replies are listed 'Best First'.
Re: Re: Simple Data storage
by PodMaster (Abbot) on Mar 08, 2004 at 12:36 UTC
    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

        there are quite a few nodes around here about installing modules for your specific username. I don't have reference to any of them on hand.. but you should be able to install these modules on your server w/o affecting anyone else's work.

        Grygonos

        Storable is no worse than require 'savefile'; in this case. I think it is included with Perl 5.6.1.

        If you print out a file then require it back in, and your escaping is not correct you're opening yourself to code injection. Storable would protect you from this.