Home > Archive > PERL Modules > October 2006 > DBM and Perl
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi,
I have tried to understand the docs to use NDBM_File, but haven't quite
understood it. I have used the following to connect to a DBM file:
#!/usr/bin/perl -w
use strict;
use NDBM_File;
my %bills;
my ($dbh, $key, $val);
$dbh = tie(%bills, 'NDBM_File', 'bills_generated', 1, 0);
while (($key, $val) = each %bills) {
print $key, ' = ', unpack('L', $val), "\n";
}
untie(%bills);
exit 0;
But I don't know how to store something in the file. The documentation
suggests that I have to create my own class, but I don't understand this
requirement. Is there not a default way to do this?
Thanks in advance.
Regards,
--
Raj Kothary :: one|concept
| |
| Mumia W. (reading news) 2006-10-17, 6:56 pm |
| On 10/17/2006 08:11 AM, Raj wrote:
> Hi,
>
> I have tried to understand the docs to use NDBM_File, but haven't quite
> understood it. I have used the following to connect to a DBM file:
>
> #!/usr/bin/perl -w
>
> use strict;
> use NDBM_File;
>
> my %bills;
> my ($dbh, $key, $val);
>
> $dbh = tie(%bills, 'NDBM_File', 'bills_generated', 1, 0);
> while (($key, $val) = each %bills) {
> print $key, ' = ', unpack('L', $val), "\n";
> }
> untie(%bills);
> exit 0;
>
> But I don't know how to store something in the file. The documentation
> suggests that I have to create my own class, but I don't understand this
> requirement. Is there not a default way to do this?
>
> Thanks in advance.
>
> Regards,
Write to the hash %bills to store something in the file.
--
paduille.4060.mumia.w@earthlink.net
| |
| John W. Krahn 2006-10-17, 6:56 pm |
| Raj wrote:
>
> I have tried to understand the docs to use NDBM_File, but haven't quite
> understood it. I have used the following to connect to a DBM file:
>
> #!/usr/bin/perl -w
>
> use strict;
> use NDBM_File;
>
> my %bills;
> my ($dbh, $key, $val);
>
> $dbh = tie(%bills, 'NDBM_File', 'bills_generated', 1, 0);
You shouldn't use "magic numbers": what do 1 and 0 stand for? You should
*ALWAYS* verify that the file opened correctly.
use Fcntl;
my $dbh = tie my %bills, 'NDBM_File', 'bills_generated', O_RDWR
or die "Cannot open 'bills_generated' $!";
> while (($key, $val) = each %bills) {
You don't need those variables in file scope:
while ( my ( $key, $val ) = each %bills ) {
> print $key, ' = ', unpack('L', $val), "\n";
> }
> untie(%bills);
> exit 0;
>
> But I don't know how to store something in the file.
(Before you untie the hash.)
$bills{ 'key' } = pack 'L', 12345;
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
|
| "John W. Krahn" <someone@example.com> wrote in message
news:MN6Zg.27457$P7.995@edtnps89...
> Raj wrote:
>
> You shouldn't use "magic numbers": what do 1 and 0 stand for? You should
> *ALWAYS* verify that the file opened correctly.
>
> use Fcntl;
> my $dbh = tie my %bills, 'NDBM_File', 'bills_generated', O_RDWR
> or die "Cannot open 'bills_generated' $!";
>
>
>
> You don't need those variables in file scope:
>
> while ( my ( $key, $val ) = each %bills ) {
>
>
>
> (Before you untie the hash.)
>
> $bills{ 'key' } = pack 'L', 12345;
Thank you and Mumia for your help...what I had read made it sound more
difficult, or maybe my interpretation did!
Do you have to pack the assignment to the hash?
Thanks
Raj
>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you can special-order
> certain sorts of tools at low cost and in short order. -- Larry Wall
| |
|
|
"Raj" <raj.kothary@thus.net> wrote in message
news:eh4s5f$jsj$1$8302bc10@news.demon.co.uk...
> "John W. Krahn" <someone@example.com> wrote in message
> news:MN6Zg.27457$P7.995@edtnps89...
>
> Thank you and Mumia for your help...what I had read made it sound more
> difficult, or maybe my interpretation did!
>
> Do you have to pack the assignment to the hash?
Sorry, please ignore the above...silly question. Thanks for your help.
Regards,
Raj
> Thanks
> Raj
>
>
>
|
|
|
|
|