sub out_iter_array { my $_aref = shift; my %_tmp; my $_order_id = 1; _croak("The argument to \"out_iter_array\" is not an array ref.") unless ( ref $_aref eq 'ARRAY' ); return sub { my $_chunk_id = shift; if ( $_chunk_id == $_order_id && keys %_tmp == 0 ) { # already orderly $_order_id++; push @{ $_aref }, @_; } else { # hold temporarily otherwise until orderly @{ $_tmp{ $_chunk_id } } = @_; while ( 1 ) { last unless exists $_tmp{ $_order_id }; push @{ $_aref }, @{ delete $_tmp{ $_order_id++ } }; } } }; }