http://qs321.pair.com?node_id=907959

rovf has asked for the wisdom of the Perl Monks concerning the following question:

The code I have works well, but I'm not entirely happy with it, so I would be glad to learn how it can be improved (shorter/clearer). This is a bit like a "programming exercise" I made up for myself, although I stumbled over this problem when writing a production program.

Problem: I have an even-sized array, with the property that the odd-numbered elements are never undef. This array should be processed left-to-right, two elements at a time, in a loop. The elements to be processed should be removed from the list. My solution goes like this:

# Array to be processed: @arr while(defined( ( my ($x,$y) = (shift(@arr), shift(@arr)) )[0])){ .... }
In this code, I combine the extraction of the first two elements from the list, with testing whether the list is already empty (which would set $x and $y to undef). Since the even-sized elements in the list are allowed to be undef, I test only $x.

However, I don't like the clumsy defined((my (...) ...)[0]). I would prefer a solution which looks as clean as the <c>each generator used to iterate over the elements of a hash. If I had this type of problem more frequently, I would perhaps write my own generator, but it would be overkill in this case. Maybe there is any other way I could improve my code?
-- 
Ronald Fischer <ynnor@mm.st>