Queues and barrel rolls in Perl
Archive - Originally posted on "The Horse's Mouth" - 2006-02-24 00:11:53 - Graham EllisThere are some applications where you want to go round and round a list and one way to do so is to "barrel roll" the list by moving the last element onto the start each time you use an element. That would be complicated in some languages, but in Perl you can simply use a pop followed by an unshift. Thus:
@hehad = ("Chicken","Feta","Rice","Sticky toffee pudding");
unshift @hehad,(pop @hehad);
And if you want a queue - simply push onto a list and unshift off ... once you get a false value back, your queue is empty.