Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #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
and the code below, I'm blocked forever (on reading from STDIN ?!!) when running it like

$ ./ sample.txt


$ ./ <sample.txt


$ cat sample.txt | ./

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

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

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (12)
    As of 2018-03-23 13:31 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (293 votes). Check out past polls.