in reply to Re: Re: Re: Why doesn't % interpolate?
in thread Why doesn't % interpolate?

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 :-(

Replies are listed 'Best First'.
Re:**($n-1) Why doesn't % interpolate?
by no_slogan (Deacon) on May 16, 2003 at 15:57 UTC
    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...)

    Is "%dth" the name of a hash, or is someone trying to printf "7th"? This solution seems like a mess. Besides, I don't want perl to start telling me what I can and can't use as a name for a particular sort of variable. Seems too much like some other language.

    It has always bothered me when people name their hashes %hash or, even worse, %hash_table. You wouldn't use $scalar or $variable, would you? I hope? Unless maybe it was a flag that told you whether some other piece of data was a scalar or a variable. The name of the variable should reflect how it's used in some way.