Hi Monks, I need to use NDBM_File db across child processes. I use below code to create DB in parent

#!/usr/bin/env perl use warnings; use Fcntl; use NDBM_File; use List::Util qw(first); my $df = 'db'; my %db; my @c_p; (tie %db, NDBM_File, $df, O_CREAT|O_RDWR, 0666) || die "$0: ERR, creat +ing DB $df : $!\n"; $db{'fields'} = ['rc', 'ev', 'h', 'u', 'oh', 'c', 'i', 'n_c']; untie %db || die "$0: Couldn't close db, $!\n"; my $pid = fork(); if ( $pid ) { # parent push @c_p, $pid; } elsif ( $pid == 0) { #child ssh_remote_exec(); exit (0); } sub ssh_remote_exec { my %_h; my @a; my %db; my $df = 'db'; (tie %db, NDBM_File, $df, O_RDWR, 0666) || die "$0: ERR, open +DB $df : $!\n"; # get output from remote exec and parse it into %_h # here open the db created in parent, use the field key to retr +ieve the array index and put there the value of key from %_h # but $db{'field'} has no data in child foreach my $k (keys %_h ) { my $i = first { $db{'fields'}->[$_] eq $k } 0..$#{$db +{'fields'}}; $a[$i] = $_h{$k}; print "key : $k, index : $i, value: $_h{$k} \n"; } print join (';', @a), "\n"; $db{'1'} = join (';', @a); untie %db || die "$0: Couldn't close db, $!\n"; }

I need to know whether I'm doing things rite ? need your wisdom in using NDBM db across multiple child processes.


In reply to use NDBM_File DB by child processes by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.