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

What would be the best database to use when MySQL is an overkill? I already have a plain text database that just stores

Name: Entry
Othername: Entry
postmaster: me@somemail.com root blah@blerf.com

But a problem with this, is that it's not robust enough. Are there any build in modules (or available on cpan) that handle PLAINTEXT (don't say DBM please) databases?

Replies are listed 'Best First'.
Re: Database?
by btrott (Parson) on May 09, 2000 at 04:55 UTC
    Yes. Take a look at DBD::CSV.

    It lets you treat a standard comma-separated value file as a SQL database and perform SQL statements on it.

    If you do eventually outgrow DBD::CSV, though (and you probably will if you do much database work), do take another look at MySQL. It's really a very fast, pretty simple database system, and I highly recommend it.

      DBD::CSV is very nice. And since it works like any other DBI driver, you won't have to completely rewrite your code when you are ready to upgrade to mySQL or some other RDBMS.
Re: Database?
by BBQ (Curate) on May 09, 2000 at 07:27 UTC
    As pointed, DBD::CSV is definitly a way to go, but if you all you want to do is simple stuff (and I mean simple), check out this node for a flatfile alternative.

    (a bit of self-promotion never hurt anyone, right? :o))

    #!/home/bbq/bin/perl
    # Trust no1!
Re: Database?
by ZZamboni (Curate) on May 09, 2000 at 17:44 UTC
    Of course, if it is a really simple format and you don't need to do anything very fancy, you could just do it manually (assuming records are separated by blank lines, that "Name" is your main key, and that "Name" appears before all other lines in each record):
    open FILE, "<file" or die "$!\n"; { local $/=""; while ($rec=<FILE>) { my @fields=split("\n", $rec); my $key; foreach (@fields) { my ($index, $value)=split(/\s*:\s*/, $_, 2); if ($index eq 'Name') { $key=$index; $db{$key}={}; } else { $db{$key}->{$index}=$value; } } } close FILE; }
    This will leave the data in %db, indexed by Name, and each element will be a hash reference with two elements called Othername and postmaster.

    --ZZamboni

      Yikes...i keep forgetting about the lack of carrage returns
      ################################
      sub ParseProfile {
      my($filename) = @_;
      open(PROFILE,$filename);
      @PROFILE = <PROFILE>;
      close(PROFILE);
      my(%profile);
      $i = 0;
      while (@PROFILE$i ne "") {
      @LINE = split(": ", @PROFILE$i);
      $value = "@LINE1"."@LINE2"."@LINE3".
      "@LINE4"."@LINE5";
      @temp = split("\n", $value);
      $value = join("", @temp);
      $profile{@LINE[0]} = $value;
      $i = $i + 1;
      }
      return %profile;
      }
      ###############################
      sub SaveProfile {
      my($profile_path, %profile) = @_;
      open(PROFILE,">$profile_path");
      while (($key, $val) = each %profile) {
      print PROFILE "$key: $val\n";
      }
      close(PROFILE);
      }
      ###############################
RE: Database?
by merlyn (Sage) on May 09, 2000 at 17:04 UTC
Re: Database?
by t0mas (Priest) on May 09, 2000 at 12:31 UTC