Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Why doesn't % interpolate?

by diotalevi (Canon)
on Apr 18, 2003 at 19:19 UTC ( [id://251517]=perlquestion: print w/replies, xml ) Need Help??

diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

It occurs to me that its an odd thing that hash variables don't interpolate like scalars and arrays. So "$var" eq $var, "@var" eq join($", @var) but "%var" eq '%var' and not "%var" eq join($", %var). What gives?

Replies are listed 'Best First'.
Re: Why doesn't % interpolate?
by no_slogan (Deacon) on Apr 18, 2003 at 19:40 UTC
    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?
      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

        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;
Re: Why doesn't % interpolate?
by chromatic (Archbishop) on Apr 18, 2003 at 19:24 UTC

    Maybe it's tricky to find the right representation. Do you want just the keys? Just the values? Keys mixed with values? Does order matter?

    Converting an array to a list is pretty sensical, but converting a hash to a list is a little different. (Think in terms of pairs.)

      Oh dunno - we already have a definition for converting a hash to a list and an interpolated list array to a scalar. So I just wonder why a hash shouldn't interpolate as a list array. Perl5 doesn't have the same ideas about pairs that Perl6 does so at this point I'm not concerned about what to do with pairs because they don't exist.

        Lists don't interpolate. (How could they? They don't have names, let alone a sigil).

        There's limited value in having hashes interpolate directly; it's not as useful as interpolation of scalars and arrays. It would be very annoying though, as you would not be able to write

        printf "%d %s %f\n", $int, $str, $float;

        because that would try to interpolate three hashes.

        However, it's still relative simple to interpolate a hash:

        print "@{[%hash]}";

        Abigail

Re: Why doesn't % interpolate?
by theorbtwo (Prior) on Apr 19, 2003 at 07:08 UTC

    Historicly, BTW, note that the interpolation of scalars has been there since the beginning (I think), whereas interpolation of arrays is rather new (5.0 partialy, normalized somwhat in 5.6, thanks, toma).


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      I'm almost certain that interpolation of arrays was available in perl4. Hmm... it was. I just tested it with perl 4.036.

      See the first paragraph in this document describing traps when going from perl4 to perl5.

      @ now always interpolates an array in double-quotish strings.
      Note the word "always". It used to be so that perl4 only interpolated arrays where they existed before being used in a string.
Re: Why doesn't % interpolate?
by dragonchild (Archbishop) on Apr 21, 2003 at 13:59 UTC
    Another problem with interpolating hashes that hasn't been mentioned yet is that it would be non-deterministic. Arrays will always return the same values, regardless of what order they were put on. (Using unshift vs. push vs. splice vs. assignment vs. etc ...) However, the same cannot be said for hashes.

    Also, adding 1..10 to an array vs. 11..20 will still return them in a predictable (a < b) order. Doing that for a hash will not return a predictable ordering.

    This matters for testing purposes - I now cannot depend on interpolation to always give me repeatable answers for testing. But, the only thing I can't depend on is hashes. Try explaining that to a newbie.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://251517]
Approved by Mr. Muskrat
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-24 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found