Print array variables with or without double quotes
When I was learning to print array variables, I found white space inserted when using double. Snippet code as shown below. Could you tell me why?
#!/usr/bin/perl -w
use strict;
use warnings;
my @str_array = ("Perl","array","tutorial");
my @int_array = (5,7,9,10);
print @str_array;
print "\n";
# added the double quotes
print "@str_array";
print "\n";
print @int_array;
print "\n";
# added the double quotes
print "@int_array";
Output:
Perlarraytutorial
Perl array tutorial
57910
5 7 9 10
+2
a source to share
2 answers
Here is perlfaq that answers your question:
Why am I getting weird whitespace when printing an array of strings?
+5
a source to share
When a variable is used inside a string, string interpolation is used. String interpolation means, among other things, the variable is formatted to become a string. When an array variable is formatted to become a string, it is given spaces between the elements. These spaces are actually defined by the $ "variable. The default value for this variable is space. For more information, see the perl documentation for special variables .
+5
a source to share