Krambambuli has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
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.
With a sample file named sample.txt that reads like
$ ./weird.pl sample.txt
whereas
$ ./weird.pl <sample.txt
or
$ cat sample.txt | ./weird.pl
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.
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.
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 ...?
Here's the code:
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.
Many thanks in advance.
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.
With a sample file named sample.txt that reads like
and the code below, I'm blocked forever (on reading from STDIN ?!!) when running it likewww1.example.com www2.example.com www3.example.com
$ ./weird.pl sample.txt
whereas
$ ./weird.pl <sample.txt
or
$ cat sample.txt | ./weird.pl
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.
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.
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 ...?
Here's the 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;
Many thanks in advance.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: <> oddity ?
by pvaldes (Chaplain) on Mar 27, 2013 at 11:11 UTC | |
by hdb (Monsignor) on Mar 27, 2013 at 11:32 UTC | |
by pvaldes (Chaplain) on Mar 27, 2013 at 11:42 UTC | |
by LanX (Sage) on Mar 27, 2013 at 11:44 UTC | |
by Krambambuli (Curate) on Mar 27, 2013 at 11:34 UTC | |
by LanX (Sage) on Mar 27, 2013 at 11:50 UTC | |
by Krambambuli (Curate) on Mar 27, 2013 at 12:18 UTC | |
by Krambambuli (Curate) on Mar 27, 2013 at 11:58 UTC | |
Re: <> oddity ?
by LanX (Sage) on Mar 27, 2013 at 10:26 UTC | |
by hdb (Monsignor) on Mar 27, 2013 at 10:31 UTC | |
by LanX (Sage) on Mar 27, 2013 at 10:48 UTC | |
by Krambambuli (Curate) on Mar 27, 2013 at 10:31 UTC | |
by LanX (Sage) on Mar 27, 2013 at 10:41 UTC | |
by hdb (Monsignor) on Mar 27, 2013 at 10:36 UTC | |
Re: <> oddity ?
by hdb (Monsignor) on Mar 27, 2013 at 11:00 UTC | |
Re: <> oddity ?
by hdb (Monsignor) on Mar 27, 2013 at 10:17 UTC |
Back to
Seekers of Perl Wisdom