DBM and Perl
am 17.10.2006 15:11:44 von raj
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
Re: DBM and Perl
am 17.10.2006 17:37:04 von paduille.4060.mumia.w
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
Re: DBM and Perl
am 17.10.2006 17:38:20 von someone
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
Re: DBM and Perl
am 18.10.2006 11:29:27 von raj
"John W. Krahn" wrote in message
news:MN6Zg.27457$P7.995@edtnps89...
> 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;
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
Re: DBM and Perl
am 18.10.2006 12:16:28 von raj
"Raj" wrote in message
news:eh4s5f$jsj$1$8302bc10@news.demon.co.uk...
> "John W. Krahn" wrote in message
> news:MN6Zg.27457$P7.995@edtnps89...
>> 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;
>
> 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
>
>>
>>
>>
>> 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
>
>