in reply to What is the correct way to use Thread::Queue::Any ?

sub consumer { while( my($returned_value) = $queue->dequeue ) { printf "I have been returned: %i\n", $returned_value; } }

I think you're being fooled by printf(). The "%i" turns an undef value into 0.

Liz

Replies are listed 'Best First'.
Re: Re: What is the correct way to use Thread::Queue::Any ?
by fx (Pilgrim) on Jan 07, 2004 at 17:45 UTC

    I thought that enqueuing an undef would have stopped that while loop and hence didn't much care about how it would interact with printf.

    Is the behaviour of enqueuing (or perhaps more specifically dequeuing) undef's under Thread::Queue::Any different than under Thread::Queue for some reason that I'm not thinking of?

    Many thanks.

      while( my($returned_value) = $queue->dequeue ) {

      You forget that $queue->dequeue always returns an array with at least 1 element. So the condition being checked is always 1 or higher. Something like:

      while( my ($returned_value) = $queue->dequeue ) { last unless defined $returned_value;
      should do the trick, I think.

      Liz

      I thought that enqueuing an undef would have stopped that while loop
      Nope, that's a list assignment in scalar context, which evaluates to the number of items on the right of the assignment.