# Start with an empty master queue. my @master; # Build a sub queue. Note that this is a array *reference*. my $subqueue = [ 1, 2, 3, 4]; # add it to the queue. push() puts it at the tail of the queue. push @master, $subqueue; # Now let's create a couple more sub queues and add them. push @master, [5, 6, 7]; # Some high-priority-items we'll stick at the front of the master: unshift @master, [-1, 0]; # Now let's run the queue. We look at the master. If there are no items remaining, # the queue is empty. Otherwise, process the leading sub queue. while (@master) { # If the leading sub queue is empty, discard it, and start over with the rest of # the (now possibly empty) master queue. while ( @{ $master[0] } ) { my $item = shift @{ $master[0] }; print $item, " "; } # We arrive here when the sub queue is empty. Discard the empty sub queue. shift @master; # Move to the next line so we can see we switched sub queues. print "\n" } print "\n";