Alexandra B. Alexandra B. - 1 year ago 81
Perl Question

Perl Threads::Queue

I'm creating an array of Thread::Queue elements.
I'm doing that like this:

for (my $i=0; $i < $queues_amount; $i++){

However, while I'm filling it each queue with elements like this


I get the following error :

Can't call method "enqueue" on an undefined value...

Could you help me with finding out what the problem is?

My perl version is 5.12.2.

Answer Source

You are not using the $ sigil on the $i variable within your loop:

for (my $i=0; $i < $queues_amount; $i++){
    $queues[i]=Thread::Queue->new;  # should be $i

If you had use strict on, the subs portion would have thrown an error about the bareword i. To catch many errors, place use warnings; use strict; at the top of all of your programs.

It is also a bit more idiomatic to write it as:

my @queues = map {Thread::Queue->new} 0 .. $queues_amount - 1;