in reply to Unknown levels of sub-element extraction

This looks like recursion to me.

sub devices_for { my ( $thing ) = @_; my @subs = query( $thing ); return map { /^DEVICE\d+/ ? $_ : devices_for( $_ ) } @subs; }

The first call, devices_for( 'GROUP1' ), causes a query( 'GROUP1' ). The map sends the device entries right through but causes a recursive call for 'GROUP2' and so forth. What the initial call for 'GROUP1' returns just a list of devices. Slip in a sort if you need them in order.

Update: Slightly better:

sub devices_for { my ( $thing ) = @_; return $thing if $thing =~ /^DEVICE\d/; return map { devices_for( $_ ) } query( $thing ); }

Or all in "one step":

sub devices_for { return ( $_[0] =~ /^DEVICE\d/ ) ? $_[0] : map { devices_for( $_ ) } query( $_[0] ); }