What is the difference between @ and $ while accessing array elements in perl?

Viewed 126

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?

1 Answers

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.

Related