home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5762 < prev    next >
Encoding:
Internet Message Format  |  1992-09-08  |  3.7 KB

  1. Path: sparky!uunet!pipex!unipalm!uknet!doc.ic.ac.uk!doc.ic.ac.uk!not-for-mail
  2. From: dds@doc.ic.ac.uk (Diomidis D Spinellis)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Using perl for databases - experiences requested
  5. Keywords: database implementation experience
  6. Message-ID: <18b01tINNiji@swan.doc.ic.ac.uk>
  7. Date: 5 Sep 92 18:59:09 GMT
  8. References: <1992Sep5.040819.2039@nugget.rmNUG.ORG>
  9. Organization: Department of Computing, Imperial College, University of London, UK.
  10. Lines: 83
  11. NNTP-Posting-Host: swan.doc.ic.ac.uk
  12.  
  13. In article <1992Sep5.040819.2039@nugget.rmNUG.ORG> scott@corybant.rmnug.org (Scott Meyer) writes:
  14. >What sort of experience have people had using perl to implement databases?   
  15.  
  16. I have used Perl to implement two databases. 
  17.  
  18. The first database is a small system I use for my thesis to keep track
  19. of various language characteristics.  I save the data using the same
  20. approach as the one you describe in your article.  Using single
  21. character field identification strings allows me to parse each record
  22. into an assiociative array of strings using the following loop:
  23.  
  24.         @rec = split(/\n\%/);
  25.         $rec[0] =~ s/^\%//;
  26.         foreach $r (@rec) {
  27.                 ($key, $val) = ($r =~ m/(.) (.*)$/);
  28.                 $rec{$key} = $val;
  29.         }
  30.  
  31. So for a record like:
  32.  
  33.     %N Perl
  34.     %A Larry Wall
  35.     %I Interpreted
  36.  
  37. $rec{N} will be Perl, $rec{A} will be 'Larry Wall', and $rec{I} will be
  38. 'Interpreted'.  Records are separated by single blank lines, so I can
  39. simply use $/=''; $*=1 to parse the input.  You mention in your article
  40. that you use filters to process complex queries.  I tried to write the
  41. query processing in the database script, but found myself to prefer the
  42. filter approach, which I am now using.  I feel uneasy about using
  43. pipelines of Perl scripts (some complex queries clock up the process id
  44. counter up by as much as 400 in an otherwise idle machine), but it
  45. seems to work fine up to now.  Most query scripts use the dbgrep Perl
  46. script which greps patterns in the database.  It is a very small script
  47. whose engine consists of the following lines:
  48.  
  49.     $/=''; $*=1;
  50.     $pattern = shift;
  51.         eval "
  52.         while (<>) {
  53.                 print if ($pattern);
  54.         }
  55.         ";
  56.  
  57. The initial Perl query processing code was based on queries in Perl syntax
  58. of the type Field-letter = value.  These were changed into Perl by the
  59. command:
  60.  
  61.     s?([$REC])\s*=\s*'([^']+)'?(((\$i) = m/%$1([^%]*)/) && \$i =~ m/$2/)?g;
  62.  
  63. and then put into an array using the command:
  64.  
  65.     $x = $_; eval "\@selected = grep($x, \@db);";
  66.  
  67. For example the query:
  68.  
  69.     select N='C' && C='Functional'
  70.  
  71. will generate the following expression to be passed through grep:
  72.  
  73.     ((($i) = m/%N([^%]*)/) && $i =~ m/C/) && 
  74.     ((($i) = m/%C([^%]*)/) && $i =~ m/Functional/)
  75.  
  76. ($i is first assigned the whole field needed, and then is matched
  77. against its contents).  The approach worked without a problem, but I
  78. found scripts much more convenient.  I wish Perl had the capability to
  79. pipe through control structures like the Bourne Shell.
  80.  
  81. The second database is a medical information system used in a hospital
  82. to track patient data.  Its user-interface is implemented in C.  The
  83. data is stored in gdbm files.  Perl scripts are used to handle query
  84. handling and report generation.  Queries are parsed from the `natural
  85. language' (do not ask me for it, you don't want it: it parses commands
  86. expressed in Greek ) syntax into a Perl expression, within a loop,
  87. which is then eval'ed.  The records are variable sized, with a key
  88. generated from the name together with a unique number.  Last time I
  89. checked there were more than 800 records in the database, and Perl was
  90. handling it without a problem.
  91.  
  92. Diomidis
  93. -- 
  94. Diomidis Spinellis    Internet: <dds@doc.ic.ac.uk>  UUCP: ...!uknet!icdoc!dds
  95. Department of Computing, Imperial College, London SW7     #include "/dev/tty"
  96.