Article 12016 of comp.lang.perl:
Path: feenix.metronet.com!news.utdallas.edu!convex!cs.utexas.edu!howland.reston.ans.net!pipex!demon!wiggins.isri.unlv.edu!grover
Newsgroups: comp.lang.perl
From:
[email protected] (Kevin Grover)
Resent-from: Gerry King <
[email protected]>
Resent-date: Sat, 26 Mar 1994 15:22:38 +0000
Subject: Re: Read/Write dBase files
X-Newsreader: TIN [version 1.1 PL8]
Resent-Date: Fri, 25 Mar 1994 09:38:54 -0800
Resent-From: Kevin Grover <
[email protected]>
Date: Fri, 25 Mar 1994 01:38:54 +0000
Message-ID: <
[email protected]>
Sender:
[email protected]
Lines: 76
In article <
[email protected]> (in comp.lang.perl) you wrote:
> Has anyone worked out how to read/write dBase files from Perl? ISTR that
> handling dbase files as associative arrays was in the wishlist. If nobody
> has tackled this I guess I'll have to have a go. I've been using Perl for
> a little while (the Llama book was a big help, great job Randal!) but always
> in text mode.
> A dbase header has a few longs and unsigneds. How do I read/write these from
> Perl?
> Gerry King
> +------------------------+------------------------------+
> | CASPE Research | voice +44 71 229 8739 |
> | 22 Palace Court | fax +44 71 221 9892 |
> | Bayswater | internet caspe.demon.co.uk |
> | London W2 4HU | |
> +------------------------+------------------------------+
use the read/write system call w/ pack and unpack
also, if you want the thing to work in DOS, you need to use the
binmode function (it places a file descriptor into binary mode -- it
is ignored on non-dos machines). there is no corrosponding asciimode
(i.e. once binmode has been used, only way to get back is close and
reopen the file)
Included below is a program called "hex", it will dump a file in HEX
(w/ printable ascii). You can use it as an example
Keep in mind, the binmode is only needed on DOS machines....
--
- kev,
[email protected]
#!/local/perl/bin/perl
# =============================================================================
# $Id: hex,v 1.1 1993/04/21 17:27:16 grover Exp $
#
# HEX in perl: dump a file in hexadecimal, and printable characters
#
# - Kevin Grover,
[email protected]
# =============================================================================
die "Usage: hex infile [infile ...]\n" unless @ARGV;
$spaces = " " x 80;
foreach $t (0..31,127..255) { $char[$t] = "."; }
foreach $t (32..126) { $char[$t] = sprintf ("%c",$t); }
while ($f = shift @ARGV) {
open (IN, "<$f") || warn "Can't open input $f: $!\n",next;
binmode IN;
&process;
$offset = 0;
close (IN);
}
sub process {
$temp = "";
while (read(IN, $temp, 16)) {
&outhex;
}
}
sub outhex {
printf ("%08X: ", $offset);
$offset += 16;
for ($i=0; $i<length($temp); $i++)
{ printf(" %02x",ord(substr($temp,$i,1)));
print " " if ($i == 7);
}
print substr($spaces, 1, (16-length($temp))*3 +2);
for ($c=0; $c<length($temp); $c++) { print $char[ord(substr($temp,$c,1))]; }
print "\n";
}