The answers above are more informative than this one will be. But your post inspired me to play with IPC::Shareable. It allows you to put variables into shared memory and access them from separate scripts. This is just a toy example and I don't have the experience required to recommend this as a solution. But it may be instructive.

It consists of two scripts, adult.pl which will run child.pl in the background. After they're both running they each update a shared array.

adult.pl:
use IPC::Shareable; use Data::Dump qw(ddx); $| = 1; # Setup shared array reference "my_var" my $id = 'my_special_name'; my %options = (create => 'yes', exclusive => 0, mode => 0644, destroy +=> 'yes'); my $my_var; tie $my_var, 'IPC::Shareable', $id, { %options } or die "tie failed: $ +!\n"; $my_var = [0..10]; system('./child.pl &'); until ($my_var->[-1] == 999) { my $num = int(rand()*100); push @$my_var, $num; ddx [ @$my_var ]; sleep 2; }; print "Leaving adult.pl..\n";
child.pl:
use IPC::Shareable; use Data::Dump qw(ddx); $| = 1; my $id = 'my_special_name'; my %options = (create => 0, exclusive => 0, mode => 0644, destroy => 0 +); my $my_var; tie $my_var, 'IPC::Shareable', $id, { %options } or die "tie failed: $ +!\n"; for (0..3) { my $num = int(rand()*100); push @$my_var, $num; ddx [ @$my_var ]; sleep 2; } push @$my_var, 999; print "Leaving child.pl..\n";
Output:
# adult.pl:20: [0 .. 10, 16] # child.pl:16: [0 .. 10, 16, 72] # adult.pl:20: [0 .. 10, 16, 72, 52] # child.pl:16: [0 .. 10, 16, 72, 52, 17] # adult.pl:20: [0 .. 10, 16, 72, 52, 17, 66] # child.pl:16: [0 .. 10, 16, 72, 52, 17, 66, 96] # adult.pl:20: [0 .. 10, 16, 72, 52, 17, 66, 96, 92] # child.pl:16: [0 .. 10, 16, 72, 52, 17, 66, 96, 92, 88] # adult.pl:20: [0 .. 10, 16, 72, 52, 17, 66, 96, 92, 88, 56] Leaving child.pl.. Leaving adult.pl..

* Note that they share data based on a named id, which means their connection can be established without the child script being started by the adult.


In reply to Re: Sharing a variable across multiple files by Loops
in thread Sharing a variable across multiple files by bihuboliya

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.