perlquestion
Krambambuli
Dear Monks,<br><br>
I just can't find the underlying reason for what I feel is a bit of an oddity and so I'm hoping that some wise monk might be able to shed light on it.<br><br>
With a sample file named sample.txt that reads like<br>
<code>
www1.example.com
www2.example.com
www3.example.com
</code>
and the code below, I'm blocked forever (on reading from STDIN ?!!) when running it like<br><br>
$ ./weird.pl sample.txt<br><br>
whereas <br><br>
$ ./weird.pl <sample.txt<br><br>
or<br><br>
$ cat sample.txt | ./weird.pl <br><br>
works perfectly OK. I'm trying to make a number of DNS requests in an async manner, controlling the maximum number of open sessions at any time and processing the names of the hosts to be checked in a 'stream-like' fashion.<br><br>
I have work-arounds, so I'm not looking for alternate solutions, but I'm just trying to understand why this code doesn't work and blocks, apparently when hitting the end of the file given on the command line.<br><br>
Am I doing something that isn't right or is Perl this time not keeping it's promesses about how files on the command line are processed ...?<br><br>
Here's the code:<br>
<readmore title="See the code">
<code>
#!/usr/bin/perl
use Data::Dumper;
use IO::Select;
use Net::DNS;
my $timeout = 10;
my $sel; # global IO::Select object
my $max_resolver = 1;
my $max_sockets = 2;
my $max_sessions = $max_resolver * $max_sockets;
my @resolvers;
push( @resolvers, Net::DNS::Resolver->new) for 1..$max_resolver;
my $resolver_counter = 0;
my $socket_counter = 0;
my $session_counter = 0;
my $host_counter = 0;
my %sockets = ();
local $| = 1;
while ( <> ) {
chomp;
print "INITIAL Line: $_\n";
if ($session_counter < $max_sessions) {
# make a new DNS session
my $resolver = $resolvers[ $resolver_counter ];
my $bgsock = $resolver->bgsend( $_, 'A' );
$sockets{ $bgsock } = $resolver;
if (defined $sel) {
$sel->add( $bgsock );
}
else {
$sel = IO::Select->new( $bgsock );
}
++$session_counter;
if (++$socket_counter == $max_sockets) {
$socket_counter = 0;
if (++$resolver_counter == $resolver_counter) {
$resolver_counter = 0;
}
}
++$host_counter;
last if $session_counter == $max_sessions;
}
next;
}
print "OKOK \$ARGV: $ARGV\n";
while ($host_counter > 0) {
my @ready = $sel->can_read( $timeout );
if ( scalar @ready > 0) {
foreach my $sock (@ready) {
my $resolver = $sockets{ $sock };
my $response = $resolver->bgread( $sock );
$sel->remove($sock);
delete $sockets{ $sock };
--$host_counter;
print "AAAA \$ARGV: $ARGV\n";
my $line = <>;
print "BBBB \$ARGV: $ARGV\n";
if ($line ) {
print "ADDITIONAL Line: $line" ;
chomp $line;
my $new_sock = $resolver->bgsend( $line, 'A' );
$sel->add( $new_sock );
$sockets{ $new_sock } = $resolver;
++$host_counter;
print "CCCC \$ARGV: $ARGV\nHostcounter: $host_counter\n";
}
}
}
else {
warn "\n\ntimed out after $timeout seconds\n\n";
}
}
exit;
</code>
</readmore>
The issue clearly has something to do with the use of IO::Select or Net::DNS. If I'm 'cutting out' that part and do only the reading and printing, everything works just as expected.<br><br>
Many thanks in advance.