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

i have to use perl to manipulate flat file database. i'am new to perl and really don't have any idea how the program should look like. is there anyone out there kind enough to give me coding example?..just a simple one..maybe something like displaying an info with certain ID. just to give me a slight idea how Perl and deliminator work. thanks a lot.

Replies are listed 'Best First'.
Re: Perl and delimiator
by yodabjorn (Monk) on Jul 03, 2002 at 02:27 UTC
    Assuming you are saying you need to read/write to a dbm I would suggest looking at DBM and DB::File (cpan)

    Otherwise if it is a Flat file of text please give us an example of the data you need to parse

    heres simple module I wrote for a project to ease reading/writeing to a DB::File. It uses storable so you can store structures in the dbfile easily.
    package easydb; use strict; use DB_File; use Storable qw(freeze thaw) ; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw(&db_open &db_close &db_store &db_fetch &db_keys); @EXPORT_OK = qw(&db_open &db_close &db_store &db_fetch &db_keys); my %db ; sub db_open { my $dbfile = shift ; tie(%db, "DB_File", $dbfile) or die "Can't open $dbfile: $!"; return \%db ; } sub db_close { untie %db ; } sub db_store { my ($key, $hashref) = @_ ; $db{$key} = freeze($hashref); return $key; } sub db_fetch { my ($key) = shift; my $ref = thaw($db{$key}) ; return $ref ; } sub db_keys { return keys %db ; } 1;
    and heres an example of how it is used in a program:
    #!/usr/bin/perl use lib('.'); use warnings; use easydb ; use Data::Dumper; use strict ; my $dbfile = "some.db" ; db_open($dbfile) ; my %legend = ( "asap.cgi" => "Journal ASAPS", "abstract" => "Journal Abstract", "query" => "Journal Query", "cen" => "CEN Report", "preprint" => "Preprint Report", "reagent2000" => "Reagent Report", "article.cgi" => "Current Journal Articles", "archive.cgi" => "Article Archive" ) ; db_store ("legend",\%legend); my $legend = db_fetch("legend"); foreach my $key (sort keys %$legend ) { print "$$legend{$key}\n"; } db_close() ; exit ;
    Not a lot of comments, but it may help ya out there

Re: Perl and delimiter
by DamnDirtyApe (Curate) on Jul 03, 2002 at 06:05 UTC

    Welcome to Perl. :-)

    If you're at all familiar with SQL, you might like DBD::CSV. It allows you to use the DBI module, the de-facto standard for talking to databases in Perl, to access a flat file as if it were a database table.


    _______________
    D a m n D i r t y A p e
    Home Node | Email
      i'm using a flat file database ('filename'.dat.txt). and the database look something like this.

      ID  AE5372;
      AC  7889099;
      DE  Human DNA;
      SQ  ACGTTTAGG....GTTA;
      
      now, i'm going tru the DBI and DBD:CVS module (thanx to u). i think this can help.(although a little blur of where to get started..).i'll get back to you if i have any further question..of course i will since i have to implement a distributed database in a cluster..still a long way to go. by the way..how bout DBD::RAM module?which one is better.?(compared with DBD::CVS).that's all for now.again...thanks for your help.