The syntax you require is already available, albeit a little obscure.

my $href; @{ $href }{ 'a' .. 'z' } = 1 .. 26; my @required_fields = qw[ a c e g i k m o q s u w y ]; for my $k ( \( @h{ @required_fields } ) ){ print $$k; } 1 3 5 7 9 11 13 15 17 19 21 23 25

Note however, that once you make $k an alias to the value, you no longer have access to the related key, so you cannot use it in your error message. This would also be the case with your 'used_as' syntax.

In order to have both the key and an alias to the value available, you would need to use syntax like

while( my( $kref, $vref ) = \( each %{ $href } ) ) { print $$kref, ' => ', $$vref; }

Which achieves this, but the problem is now that you have lost the ability to constrain the keys over which you iterate. About the best you might do in this case is to add a next if .... clause in the loop, but comparing each keys against every entry in your @required_fields array is ludicrous:). So now you might create a lookup (another hash) for just the required keys and grep against that but....:)

I keep thinging about P6s Pair and Binding operators ==> and <==, and trying to work out in my minds eye how these might be combined with a hash slice to achieve your goal, but whilst I can follow and relish TheDamians exogesies, remembering enough of the syntax and semantics to start trying to put together my own snippets is well beyond me at this stage. Maybe he'll happen by and put us out of our misery!


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


In reply to Re: Will Perl 6 abbreviate this indirection for me? by BrowserUk
in thread Will Perl 6 abbreviate this indirection for me? by princepawn

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.