in reply to Why doesn't % interpolate?

If % interpolated, printf would be kind of hard to use, wouldn't it?
printf "%d\n", $x; # interpolate %d? printf '%d\n', $x; # don't replace \n? printf "\%d\n", $x; # ick printf '%d'."\n", $x; # double ick
Perl6 is supposed to allow %vars to interpolate... anyone heard the latest thinking on how they're going to keep printf sane?

Replies are listed 'Best First'.
Re: Re: Why doesn't % interpolate?
by abstracts (Hermit) on Apr 19, 2003 at 19:40 UTC
    If % interpolated, printf would be kind of hard to use, wouldn't it?
    This does not explain why % does not interpolate; rather it shows that the choice of % as a meta character in printf is a bad one given that % is a sigil. If pritnf used a different meta character (^ for example), then this would work:
    printf "^d\n", $x; # no problem printf "%d\n", $x; # interpolate, no problem printf '^d\n', $x; # ok printf '%d\n', $x; # does not interpolate, ok too
    So, you can make % interpolate without making printf any harder to use (using ^ instead of % is not harder, just different), you just rather keep printf the way it is (so that people don't get angry).
      Well, you should realize that printf was in Perl before hashes were first class objects with their own sigil.

      Abigail

        Okay, I can change the post to say "why was % selected as a sigil given that it was a printf meta character and would cause interpolation problems?". I don't think the answer to that is short-sightedness.

        The reason for % not interpolating has got to be something other than "because printf would be harder to use". chromatic's answer seems to provide a better explanation.

        Update: What I meant to say is that if Larry wanted hashes to interpolate, he could've easily done so without having a major problem with printf (either by chosing another sigil for hashes or by changing printf's meta character).

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