2 issues with "tie"

Discussion in 'Perl Misc' started by Tuc, Aug 27, 2007.

  1. Tuc

    Tuc Guest

    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
     
    Tuc, Aug 27, 2007
    #1
    1. Advertising

  2. 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
     
    comp.llang.perl.moderated, Aug 27, 2007
    #2
    1. Advertising

  3. Tuc

    Ben Morrow Guest

    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, <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, Aug 27, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Weekes

    Tie to change line in file

    Jonathan Weekes, Sep 14, 2003, in forum: Perl
    Replies:
    0
    Views:
    496
    Jonathan Weekes
    Sep 14, 2003
  2. Carlo Filippini

    Tie a file handle to a variable

    Carlo Filippini, Sep 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    444
  3. Carlo Filippini

    Tie a file handle to a variable

    Carlo Filippini, Sep 29, 2003, in forum: Perl
    Replies:
    3
    Views:
    635
    Kris Wempa
    Oct 1, 2003
  4. Antonio Gulli

    Tie a large list of DB_Files

    Antonio Gulli, Dec 27, 2003, in forum: Perl
    Replies:
    0
    Views:
    462
    Antonio Gulli
    Dec 27, 2003
  5. botfood

    tie() with DB_File not tie()ing ?

    botfood, Apr 24, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    460
    botfood
    Apr 26, 2006
Loading...

Share This Page