# PSEUDOCODE ... our $MAXTHREADS = 5; our @aryItems : shared; ... main(); sub main {...} sub process { my $threads = 0; my $files = (@aryItems); if ($files > $MAXTHREADS) { $threads = $MAXTHREADS; } initThreads($threads, ...); ... } sub initThreads { ... my $threads = 0; ... ($threads, ...) = @_; our @threads = (); for (0..$threads - 1) { push @threads, threads->new( \&Foo::run, id => $_, ... ); } } ... #### Foo class #### package Foo; use threads; use threads::shared; ... sub new{...}; sub run{};