I think it only shows that there are problems with interpolating hashes. There's a similar problem that is solved quite very clever. Using many backreferences in regexes create also an ambigious situation if you would use more than 9 backreferences, I quote from perldoc perlre:

There is no limit to the number of captured substrings that you may use. However Perl also uses \10, \11, etc. as aliases for \010, \011, etc. (Recall that 0 means octal, so \011 is the character at number 9 in your coded character set; which would be the 10th character, a horizontal tab under ASCII.) Perl resolves this ambiguity by interpreting \10 as a backreference only if at least 10 left parentheses have opened before it. Likewise \11 is a backreference only if at least 11 left parentheses have opened before it. And so on. \1 through \9 are always interpreted as backreferences.

I think such a behaviour should be also possible for hash interpolation. We could chosse not to interpolate %i, %d, ..., but interpolate all non used formats from printf like %hash, ... . (Most of the %i, %d, ... aren't even good names for hashes, and the most sensful abbreviation for a hash %h isn't blocked by printf). Another way could be to interpolate %i, %d, ... only if there is such a hash defined. And even as also %llf can be a valid name, when working with quads, it's not that big problem. (Especially as such ambigious situations could be detected by the warning pragma)

Of course, it is an argument, that it is hard for Perl to find out what a kind of interpolation makes sense for the user. I would guess it would be key1=>value1,key2=>value2,.... However, it should be not difficult to customize this behaviour like it is already done for arrays with the $" variable. We only need a variable specifying the seperator between key and value ("=>"), between elements (","), and whether you want to see keys, values or both. The only disappointing fact to this idea is that the $% variable is already forgiven :-(

I cannot imagine that it is so hard to implement. Perhaps it's an idea to bundle it in Perl 5.10. I would find it as a real cool feature and free us from using Data::Dumper or similar. Especially in 1-4 liners, it's frustrating to type so much sensless chars, only to print a hash!

Greetings,
Janek
PS: I didn't post this message already in april, as I've been not on perlmonks in april :-(


In reply to Re: Re: Re: Re: Why doesn't % interpolate? by bigj
in thread Why doesn't % interpolate? by diotalevi

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.