2 issues with "tie"

2 issues with "tie"

am 27.08.2007 03:32:41 von tuctboh

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

Re: 2 issues with "tie"

am 27.08.2007 18:57:33 von Charles DeRykus

On Aug 26, 6:32 pm, Tuc 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

Re: 2 issues with "tie"

am 28.08.2007 00:40:49 von Ben Morrow

Quoth "comp.llang.perl.moderated" :
> On Aug 26, 6:32 pm, Tuc 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.

Under FreeBSD, is in fact implemented with 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]