Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^5: Storable and passed filehandles (ref to glob)

by tye (Sage)
on Aug 16, 2005 at 00:40 UTC ( [id://484032]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Storable and passed filehandles (ref to glob)
in thread Storable and passed filehandles

Quoting the Storable docs:

At the cost of a slight header overhead, you may store to an already opened file descriptor using the store_fd routine, and retrieve from a file via fd_retrieve.

So you probably wrote that file with something other than store_fd and so it doesn't contain the "slight header overhead" that fd_retrieve is expecting.

- tye        

  • Comment on Re^5: Storable and passed filehandles (ref to glob)

Replies are listed 'Best First'.
Re^6: Storable and passed filehandles (ref to glob)
by blahblah (Friar) on Aug 16, 2005 at 01:21 UTC
    The giant.db is created by the sysopen. The only thing that ever writes to it is the store_fd.

    Thanks.

      Then you are trying to retrieve from an empty file, which fails so nothing ever gets stored to it.

      - tye        

        and if I was more careful about analyzing error output I would have seen that. Damn tye, you rock.

        So fd_retrieve doesn't work on an empty file, it only works on storable data. I would think it should return an empty hashref - but hey, it doesn't and I can deal with that now that I know it.

        The working revised code:
        #!/usr/bin/perl -w # start with no existing giant.db file use strict; use Fcntl qw(:DEFAULT :flock); use Storable qw(store_fd fd_retrieve retrieve); use Data::Dumper; $Data::Dumper::Deepcopy=1; $Data::Dumper::Purity=1; $Data::Dumper::Sortkeys=1; my %data = ( 1 => 'FEE', 2 => 'FYE', 3 => 'FOH', 4 => 'FUM', ); my $exists = (-e "giant.db"?1:0); sysopen(*DB, "giant.db", O_RDWR|O_CREAT, 0666) or die("sysopen: $!\n") +; flock(*DB, LOCK_EX) or die("flock: $!\n"); my $hashref = $exists?fd_retrieve(*DB):{}; # modify the data, do work, etc.. $hashref->{$_} = $data{$_} for sort keys %data; store_fd($hashref, *DB) or die("store_fd: $!\n"); truncate(*DB, tell(*DB)); close(*DB);

        and you can check the resultant giant.db file on the command line with:
        perl -MStorable -MData::Dumper -e '{print Dumper(retrieve("giant.db")) +}'

        Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://484032]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-26 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found