Here is an example using threads and threads::shared, I am on Win32 and ActiveState 5.8.0
You could use pipe without much effort for a parent and single child communication.. but things get really messy when you try to fork off 10 or 20 processess, and have the children all communicate async with the parent or even each other, I think merlyn or one of the other "gods" has a solution for communication and even directing multi-forked processes. You should also check out Parallel::ForkManager for a very neat way to manage forked processes:
use strict;
use threads;
use threads::shared;
my @list = qw (
www.apple.com www.ibm.com www.oracle.com www.sun.com
);
my @good: shared;
my @bad: shared;
my $thread;
my @thread;
foreach(@list){ push @thread, threads->new("ping", "$_"); }
foreach $thread( @thread){ $thread->join; }
print "Good: @good\n";
print "Bad: @bad\n";
sub ping {
my $ip = shift;
my @rv = `ping -n 1 -w 100 $ip`;
foreach(@rv){
if (/(\d+\.\d+\.\d+\.\d+)/){ $ip = $1 };
push @good, $ip if /\(0% loss\)/g;
push @bad, $ip if /\(100% loss\)/g;
}
}
Cheers,
JamesNC
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.