6

What are the differences between @variable and $variable in Perl?

I have read code with the symbol $ and the symbol @ before a variable name.

For example:

$info = "Caine:Michael:Actor:14, Leafy Drive";
@personal = split(/:/, $info);

What are the difference between a variable containing $ as opposed to @?

Antti29
  • 2,953
  • 12
  • 34
  • 36
makalshrek
  • 853
  • 3
  • 14
  • 29
  • 1
    `$`- scalar, `@` - array. `$info` is a scalar string containing `:`, @personal - array to wich the return value of split function is assigned. `split` takes two argument (delimiter, valueToSplit) – d.k Aug 01 '12 at 07:22
  • 1
    Duplicate of http://stackoverflow.com/questions/2731542/what-is-the-difference-between-this-that-and-those-in-perl – daxim Aug 01 '12 at 11:23
  • 1
    There is a very good and concise description in **[perlintro](http://perldoc.perl.org/perlintro.html#Perl-variable-types)**! – rubber boots Aug 01 '12 at 11:25

6 Answers6

7

It isn't really about the variable, but more about the context how the variable is used. If you put a $ in front of the variable name, then it is used in scalar context, if you have a @ that means you use the variable in list context.

  • my @arr; defines variable arr as array
  • when you want to access one individual element (that is a scalar context), you have to use $arr[0]

You can find more about Perl contexts here: http://www.perlmonks.org/?node_id=738558

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Benedikt Köppel
  • 4,853
  • 4
  • 32
  • 42
  • Good answer. You can also think of `$` as `This` (*This number is 42* <=> `$number = 42`) and of `@` as `These` (*These values are (1 .. 42)* <=> `@values = (1 .. 42)` and *This last value* <=> `$values[-1]`) – amon Aug 01 '12 at 09:44
  • 1
    `@arr` is **array**, not **list**. see behavior: `print $s = @all = qw(my list here);` and `print $s = qw(my list here);` – gaussblurinc Aug 01 '12 at 10:03
  • i want to hear about list and scalar contexes of single-slices, please. does here `@one[$one]` `@` means list context? it is not true in all situations – gaussblurinc Aug 02 '12 at 09:35
6

All your knowledge about Perl will be crashed with mountains, when you don't feel context of this language.

As many people, you use in your speech single value (scalars) and many things in a set.

So, the difference between all of them:

i have a cat. $myCatName = 'Snowball';

it jump on bed where sit @allFriends = qw(Fred John David);

And you can count them $count = @allFriends;

but can't count them at all cause list of names not countable: $nameNotCount = (Fred John David);

So, after all:

print $myCatName = 'Snowball';           # scalar
print @allFriends = qw(Fred John David); # array! (countable)
print $count = @allFriends;              # count of elements (cause array)
print $nameNotCount = qw(Fred John David); # last element of list (uncountable)

So, list is not the same, as an array.

Interesting feature is slices where your mind will play a trick with you:

this code is a magic:

my @allFriends = qw(Fred John David);
$anotherFriendComeToParty =qq(Chris);
$allFriends[@allFriends] = $anotherFriendComeToParty; # normal, add to the end of my friends
say  @allFriends;
@allFriends[@allFriends] = $anotherFriendComeToParty; # WHAT?! WAIT?! WHAT HAPPEN? 
say  @allFriends;

so, after all things:

Perl have an interesting feature about context. your $ and @ are sigils, that help Perl know, what you want, not what you really mean.

$ like s, so scalar
@ like a, so array

gaussblurinc
  • 3,642
  • 9
  • 35
  • 64
4

Variables that start $ are scalars, a single value.

   $name = "david";

Variables that start @ are arrays:

   @names = ("dracula", "frankenstein", "dave");

If you refer to a single value from the array, you use the $

   print "$names[1]"; // will print frankenstein
matt freake
  • 4,877
  • 4
  • 27
  • 56
3

From perldoc perlfaq7

What are all these $@%&* punctuation signs, and how do I know when to use them?

They are type specifiers, as detailed in perldata:

$ for scalar values (number, string or reference)
@ for arrays
% for hashes (associative arrays)
& for subroutines (aka functions, procedures, methods)
* for all types of that symbol name. In version 4 you used them like
  pointers, but in modern perls you can just use references.
Zaid
  • 36,680
  • 16
  • 86
  • 155
2

$ is for scalar variables(in your case a string variable.) @ is for arrays.

split function will split the variable passed to it acoording to the delimiter mentioned(:) and put the strings in the array.

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 1
    Pedantically, the `split` function turns the scalar into a *list*. The assignment then stores that list in an array. – Dave Cross Aug 01 '12 at 08:56
  • $n is a scalar(here an index of an array) and $array[$n] will be another scalar variable. – Vijay Aug 02 '12 at 06:17
1
Variable name starts with $ symbol called scalar variable.
Variable name starts with @ symbol called array.

$var -> can hold single value.
@var -> can hold bunch of values ie., it contains list of scalar values.
Saravanan
  • 119
  • 10