O' Great Bearers of Perl Wisdom...
I want to use a variable in the FILEHANDLE in order to allow the number of pipes that I create to be flexible. The sample code that I have included is just an example of creating dynamic FILEHANDLEs. It doesn't work because perl doesn't recognize the FILEHANDLE with the added variable. This is eventually leading into how I use fork and return the results back to the parent process.
use strict;
my %hash;
my @dirs;
@dirs = qw(ONE TWO THREE FOUR);
for my $loop(@dirs)
{
pipe (FROM_CHILD_$loop, TO_PARENT_$loop) or die "Oops Pipe: $!\n";
select((select(TO_PARENT_$loop), $|++ )[0]); # set autoflush
$|++;
my $test = rand 5; # simulate processing
print TO_PARENT_$loop "$test $loop\n";
}
for my $loop(@dirs)
{
chomp( my $in = <FROM_CHILD_$loop> );
(my $value, my $key) = split(/\ /, $in);
$hash{$key} = $value;
}
close FROM_CHILD_$loop;
close TO_PARENT_$loop;
for my $loop(keys %hash)
{
print "keys $loop => $hash{$loop}\n";
}
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.