For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > August 2007 > 2 issues with "tie"









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]

 

Author 2 issues with "tie"
Tuc

2007-08-26, 10:05 pm

Hi,

I seem to be running into some issues with tie in perl 5.8.8 with the
defined-or patch from the FreeBSD ports.

I use :
use NDBM_File;
use Fcntl;

I open up my file as :
tie (%fdb,'NDBM_File',"file.victims",O_RDWR|O_CREAT,0777) ||die $!;

I write to it as
$fdb{$_}="TUC";

I close it as :
untie %fdb;

Seems pretty basic. But I've got 2 issues.

1) If $_="Fred", and the program iterates and does
$fdb{'Fred'}="TUC"; , if I immediately start another program up to
read "Fred" from the file, it claims it doesn't exist. As if it hasn't
sync'd. As soon as I do the "untie %fdb;", then the data becomes
available. Is there a way that as soon as I $fdb{'Fred'}="TUC"; it
becomes accessible?

2) I also have the issue that if I'm running my program, and I hit
CNTRL-C to stop it, none of the values I've $fdb{$_}="TUC"; end up
getting and staying set. So the next time the program runs, and checks
to see if its already in the data store, it says its not and re-
executes the command. Is there some way that once its set its
"committed" per se?

Thanks, Tuc

comp.llang.perl.moderated

2007-08-27, 7:52 pm

On Aug 26, 6:32 pm, Tuc <tuct...@gmail.com> wrote:

> I seem to be running into some issues with tie in perl 5.8.8 with the
> defined-or patch from the FreeBSD ports.
>
> I use :
> use NDBM_File;
> use Fcntl;
>
> I open up my file as :
> tie (%fdb,'NDBM_File',"file.victims",O_RDWR|O_CREAT,0777) ||die $!;
>
> I write to it as
> $fdb{$_}="TUC";
>
> I close it as :
> untie %fdb;
>
> Seems pretty basic. But I've got 2 issues.
>
> 1) If $_="Fred", and the program iterates and does
> $fdb{'Fred'}="TUC"; , if I immediately start another program up to
> read "Fred" from the file, it claims it doesn't exist. As if it hasn't
> sync'd. As soon as I do the "untie %fdb;", then the data becomes
> available. Is there a way that as soon as I $fdb{'Fred'}="TUC"; it
> becomes accessible?


NDBM doesn't appear to have a 'sync' method to
force a flush to disk. I don't know if there's
a convenient workaround so, alternatively, you
may want to consider using DB_File which does
provide a 'sync'.

DB_File also has other advantages and doesn't
have have NDBM's key,value length max.

>
> 2) I also have the issue that if I'm running my program, and I hit
> CNTRL-C to stop it, none of the values I've $fdb{$_}="TUC"; end up
> getting and staying set. So the next time the program runs, and checks
> to see if its already in the data store, it says its not and re-
> executes the command. Is there some way that once its set its
> "committed" per se?
>


You could set up an signal handler to catch
the interrupt and untie, eg.,

$SIG{INT} = sub { untie %fdb; };

--
Charles DeRykus

Ben Morrow

2007-08-27, 7:52 pm


Quoth "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>:
> On Aug 26, 6:32 pm, Tuc <tuct...@gmail.com> wrote:
>
>
> NDBM doesn't appear to have a 'sync' method to
> force a flush to disk. I don't know if there's
> a convenient workaround so, alternatively, you
> may want to consider using DB_File which does
> provide a 'sync'.
>
> DB_File also has other advantages and doesn't
> have have NDBM's key,value length max.


Under FreeBSD, <ndbm.h> is in fact implemented with <db.h> anyway, so
switching to DB_File should leave you still able to read your old
databases.

Ben

--
"Awww, I'm going to miss her."
"Don't you hate her?"
"Yes, with a fiery vengeance."
[ben@morrow.me.uk]
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com