Re: How to persist and query a hash lookup in memory with Perl ?
- From: Jack <jack_posemsky@xxxxxxxxx>
- Date: Thu, 29 Nov 2007 17:06:50 -0800 (PST)
On Nov 29, 3:35 pm, Ben Morrow <b...@xxxxxxxxxxxx> wrote:
Quoth Jack <jack_posem...@xxxxxxxxx>:
<snip: some sort of persistant shared memory>
Thanks I am willing to install Perl Modules, really wondering if there
is a native ability to do it out of the box in Perl.
Well, on systems that support it you can use SysV shared memory, for
which Perl natively has the raw shm* calls. This doesn't work on
Windows, though.
You could try IPC::Shareable for Unix and Win32::MMF::Shareable (which
has the same interface) for Win32, which provide an easier interface to
shared memory.
Another alternative would be mmap, using the Sys::MMap module. mmap is
substantially better behaved than SysV shm, and portable to Windows.
I got persistent memory to work but its not clear how to "pin" a hash
lookup into memory since .pl files and the persistent memory code
example itself are only as good as the life of the execution of the
script !!
In what sense then is this memory 'persistent'? I think you are rather
confused about what is and isn't possible under modern operating
systems: unless you take steps to prevent it, all memory allocated by a
process is released when that process exits. This is a feature :).
Ben
ok.. lets talk with code - I got this to work with DB_File.. my
fundamental question is, once you create the tied hash like the below,
and the program is done executing, how do I access the hash and query
it from another Perl execution program ?? I tried using the API
interface afterwards as described here http://perldoc.perl.org/DB_File.html
, see the error at bottom:
### RUN PROGRAM 1 #####
use warnings ;
use strict ;
use DB_File ;
our (%h, $k, $v) ; unlink "fruit" ;
tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0666, $DB_HASH
or die "Cannot open file 'fruit': $!\n"; # Add a few key/
value pairs to the file
$h{"apple"} = "red" ;
$h{"orange"} = "orange" ;
$h{"banana"} = "yellow" ;
$h{"tomato"} = "red" ; # Check for existence of a key
print "Banana Exists\n\n" if $h{"banana"} ; # Delete a key/
value pair.
delete $h{"apple"} ; # print the contents of the file
while (($k, $v) = each %h)
{ print "$k -> $v\n" }
untie %h ;
#####START SEPARATE PROGRAM 2 ###
use warnings ;
use strict ;
use DB_File ;
$db = tie %h, "DB_File", "testfile1" ;
foreach (keys %h)
{ print "$_\n" }
### PROGRAM 2 ERRORS:
Global symbol "$db" requires explicit package name at
test_dbfile1_query.pl .
Global symbol "%h" requires explicit package name at
test_dbfile1_query.pl .
Global symbol "%h" requires explicit package name at
test_dbfile1_query.pl 5.
Execution of test_dbfile1_query.pl aborted due to compilation errors.
.
- Follow-Ups:
- Re: How to persist and query a hash lookup in memory with Perl ?
- From: Gunnar Hjalmarsson
- Re: How to persist and query a hash lookup in memory with Perl ?
- References:
- How to persist and query a hash lookup in memory with Perl ?
- From: Jack
- Re: How to persist and query a hash lookup in memory with Perl ?
- From: jackposemsky
- Re: How to persist and query a hash lookup in memory with Perl ?
- From: Jim Gibson
- Re: How to persist and query a hash lookup in memory with Perl ?
- From: Jack
- Re: How to persist and query a hash lookup in memory with Perl ?
- From: Ben Morrow
- How to persist and query a hash lookup in memory with Perl ?
- Prev by Date: Re: Sorted Hash
- Next by Date: Re: Sorted Hash
- Previous by thread: Re: How to persist and query a hash lookup in memory with Perl ?
- Next by thread: Re: How to persist and query a hash lookup in memory with Perl ?
- Index(es):
Relevant Pages
|
|