Greetings ewhitt,
Forking is nice, but when I need data back to the parent, I like using threads instead. The communication between source and thread is simple, only once at the end, but it usually does what I need, and it's pretty intuative.
If you need communication before the end of the thread, you can set that up with shared data structures with threads::shared.
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use IO::File;
use Net::SSH 'sshopen2';
use DBI;
# username@hostname connect strings
my @servers = qw( gryphon@hornet gryphon@kursk gryphon@garfield );
# startup a thread for each server
my @threads = map {
threads->new(
sub {
my ($server) = $_[0];
my ( $reader, $writer ) = ( IO::File->new(), IO::File->new
+() );
# ssh to the server and run "ls"
sshopen2( $server, $reader, $writer, 'ls' ) or die "ssh fa
+iled $!";
my @rv;
while ( <$reader> ) {
chomp();
push @rv, $_;
}
# return the server name and an array ref of filenames
return $server, \@rv;
},
$_,
);
} (@servers);
# usual DBI stuff
my $dbh = DBI->connect(
'DBI:mysql:database=DATABASENAME;host=localhost;port=3306',
'USERNAME', 'PASSWORD', { 'RaiseError' => 1, 'AutoCommit' => 1 },
) or die $DBI::errstr;
my $sth = $dbh->prepare('INSERT INTO results (server, filename) VALUES
+ (?,?)');
# wait for threads to complete; insert data into db
foreach (@threads) {
my ( $server, $files ) = $_->join();
$sth->execute( $server, $_ ) foreach ( @{ $files } );
}
$dbh->disconnect();
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.