Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Perl support interpolation of hashes in double quotes?

#!/usr/bin/perl
use warnings;

my %hash=("no1"=>1, 
        "no2"=>2,
      );

print %hash; #Prints no11no22
print "%hash"; #Prints %hash

Why doesn't Perl support interpolation of a hash within double quotes? It supports interpolation for scalars ($), arrays (@) then why not for hashes (%)?

like image 861
Chankey Pathak Avatar asked Jul 18 '11 10:07

Chankey Pathak


People also ask

Does Perl allow variable interpolation?

This page shows how variable interpolation works in Perl. Interpolation, meaning "introducing or inserting something", is the name given to replacing a variable with the value of that variable.

How do you escape a double quote in Perl?

If you put a back-slash \ in a double-quoted string, Perl will think you want to escape the next character and do its magic.

What is Perl interpolation?

Interpolation in Perl refers to the process where the respective values replace the variable names. It is commonly used inside double-quoted strings.


2 Answers

To quote Nathan Torkington: "The big problem is that % is heavily used in double-quoted strings with printf." More information is here.

like image 62
Kilian Foth Avatar answered Oct 14 '22 13:10

Kilian Foth


How should a hash stringify? Scalars are obvious and arrays too. But what should a hash be? How useful will such a stringification be? Is it more or less useful than being able to use a % character unescaped in an interpolating string? Is it worth the amount of work it will take to fix all of the code that uses % in interpolated strings today?

If you can come up with good answers to these questions, then I am sure P5P would be willing to listen to them.

like image 15
Chas. Owens Avatar answered Oct 14 '22 12:10

Chas. Owens