The only thing you can "return" from a child process is an exit code, which is simply the value sent from your exit call, but even then, you have to match this number up with the process in your signal handler, and so forth, so it's not very practical at all.

You will certainly want to read the perlipc documents, those that deal with Inter-Process Communication. This is a fairly in-depth document, which can be confusing, but there are some good examples.

Since the parent and the child are separate processes, they can't communicate with eachother through variables, like you can within a single program. Instead, you have to create some sort of mechanism for communication explicitly. Generally, this involves creating a pipe that is shared the parent and child. This data conduit is used by the child to send any required data back to the parent.

Basically, the parent creates a pipe, which is double-ended, and then forks. The child will use one end of the pipe to report, and the parent will use the other end to listen. It's kind of like a tin-can and string telephone.

Of course, the parent must check on these sockets for each child that is active. You can use select, or, if you're feeling rather spirited, IO::Select may be more to your liking since it is simpler. The idea is that while each child has a single end, the parent is going to have a whole lot of ends to check on, as data could come in at any time. IO::Select will let you know when something comes in on one of them.

There is a good example of creating the pipe in the section of perlipc entitled "Bidirectional Communication with Yourself". There

A simpler approach, which is more of a hack, really, is to just save some data in a temp file (i.e. "/tmp/$$.progname") and have the parent pick it up when the child is done. I would certainly have a go with the pipes before taking these comparatively drastic measures, though.

In reply to Re: forking: retaining variables after the child exits by tadman
in thread forking: retaining variables after the child exits 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.