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

This does not explain why % does not interpolate...

I think it shows why making hashes interpolate would be a bad idea (given the current state of affairs), which amounts to the same thing.

... rather it shows that the choice of % as a meta character in printf is a bad one given that % is a sigil

Unfortunately, it's hard to make all those design choices up front, and now we're pretty much stuck with them. Larry could decide to change the printf escape character for Perl6. It would make it difficult to mechanically translate Perl5 programs that use non-constant printf format strings to Perl6, but maybe he'd find that acceptable. Changing the hash sigil at this point would be a much bigger problem.

printf '%d\n', $x;

To make this work, you'd need to have printf itself interpret the \n escape. That could be done, but it makes me nervous. Newbie programmers seem to have a hard enough time understanding escape sequences without muddying the water this way. I can just hear it: "Why does printf 'foo\n' work, but not print 'foo\n'?" Plus, if you actually wanted to printf a literal backslash, you'd need to put four backslashes in your code. Yuck.

Maybe there could be a new printf directive for a newline.

printf '%d%n', $x;

Replies are listed 'Best First'.
Re: Re: Re: Re: Why doesn't % interpolate?
by bigj (Monk) on May 16, 2003 at 14:31 UTC
    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:

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

      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.