I think this is possible, however my solution is cryptic and ugly, but it works. I recommend you to copy-paste the following snippets to a syntax-highlighting editor, otherwise you will get lost. Let's start with an easier version that still uses a variable for holding a coderef to an anonymous sub, later on we will eliminate it.
#! /usr/bin/perl use strict; use warnings; my $f = sub { return @_ == 1 ? undef : ( [ splice @_, -2 ], @_ == 1 ? () : &{ $_[0] }(@_) ); }; map { print "$_->[0] -> $_->[1]\n" } $f->( $f, %{ { x => 5, y => 8, z => 42 } } );
A little explanation: I'm defining a recursive anonymous sub, that gets its coderef as its first argument, and the hash listified as the following arguments. Then returns undef for the recursion base case (@_==1), or returns an arrayref for its last two args, and calls itself for the remaining args (if there are enough remaining args). Okay, try it, the result is this:
z -> 42 x -> 5 y -> 8
Then eliminate the var for the coderef: replace the occurances of $f in the map statement with the former sub{} definition and we get this:
#! /usr/bin/perl use strict; use warnings; map { print "$_->[0] -> $_->[1]\n" } &{ sub { return @_ == 1 ? undef : ( [ splice @_, -2 ], @_ == 1 ? () : &{ $_[0] }(@_) ); } }( sub { return @_ == 1 ? undef : ( [ splice @_, -2 ], @_ == 1 ? () : &{ $_[0] }(@_) ); }, %{ { x => 5, y => 8, z => 42 } } );
I'm sure you don't want to use this in real code, however this shows the idea. And sorry but I cannot see any easier solution at this moment.

In reply to Re: Iterating over verbatim hash reference by rubasov
in thread Iterating over verbatim hash reference by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.