2

I have 2 basic questions on perl variables.

@names = ('Super_!','Massive_@',"Black#", "Hole*");
print "Top Player: @names[0]\n";
print "Top Player: $names[0]\n";

Although there is no difference between output, what is the actual difference while trying to access elements @ and $.

Also, while declaring arrays of strings, what is the difference between defining element in single quote vs double quote?

  • [Already answer here](https://stackoverflow.com/questions/15726058/in-perl-what-is-the-difference-between-accessing-an-array-element-using-ai) – ssr1012 Jun 13 '20 at 11:41
  • Please ask unrelated questions in separate Questions. – ikegami Jun 14 '20 at 01:46

1 Answers1

0

As you know @ sign points out to array and $ sign to scalar.

Since you going to access an element of an array which is scalar it's preferred to use $ otherwise you will get a warning if you defined use warnings;.

In other words, in Perl variable's prefix represents what you want to have, no what you have.

--

Single ' and double quotations " both used to define strings but double-quotation "hello $name" allows you to use string interpolation.

Mahdi Zarei
  • 155
  • 7
  • This answer is wrong. An `@` means multiple items, and `$` means a single item. That's why the single element access of an array is `$array[$i]` and a hash slice is `@hash{@keys}`. – brian d foy Jun 14 '20 at 09:38