Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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
www1.example.com www2.example.com www3.example.com
and the code below, I'm blocked forever (on reading from STDIN ?!!) when running it 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:
#!/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;
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.

In reply to <> oddity ? by Krambambuli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found