Hi Monks,

I wanted to share an array between to processes created by fork(), but the following code is broken and I have no clue why:

use strict; use diagnostics; use IPC::Shareable; use POSIX ":sys_wait_h"; my @data; my %options = ( create => 1, exclusive => 0, mode => 0644, destroy => 1, ); my $pid; my $item; if ( ! defined ( $pid = fork() ) ) { die( "Cannot fork!: $!" ) } elsif ( $pid == 0 ) { # Child #tie( @data, 'IPC::Shareable', 'glue', \%options ); sleep 1; for ( my $i=1; $i <= 10; $i++ ) { $item = shift( @data ); if ( ! defined $item ) { $item = '' } print "Child: $item\n"; } } else { # Parent tie( @data, 'IPC::Shareable', 'glue', \%options ); for ( my $i=1; $i <= 10; $i++ ) { push( @data, $i ); print "Parent: @data\n"; } waitpid( $pid, WNOHANG ); }
It yields:
Not an ARRAY reference at /usr/local/share/perl/5.20.2/IPC/Shareable.p +m line 362 (#1) (F) Perl was trying to evaluate a reference to an array value, but + found a reference to something else instead. You can use the ref() func +tion to find out what kind of ref it really was. See perlref. Uncaught exception from user code: Not an ARRAY reference at /usr/local/share/perl/5.20.2/IPC/Sha +reable.pm line 362. IPC::Shareable::PUSH(IPC::Shareable=HASH(0x2604088), 1) called + at test_2.pl line 45 xyz@v32470:~/trans_gpsd/bin$ Child: Child: Child: Child: Child: Child: Child: Child: Child: Child:

Any idea?

Cheers

Bloehdian


In reply to IPC:Shareable: Not an array reference by Bloehdian

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.