Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "@" and "$" in Perl

Tags:

perl

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 @?

like image 700
makalshrek Avatar asked Aug 01 '12 07:08

makalshrek


People also ask

What is difference between and in Perl?

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

What is the difference between -> and => in Perl?

The former is a function call. The latter is a method call.

What does ~~ mean in Perl?

Just as with the =~ regex match operator, the left side is the "subject" of the match, and the right side is the "pattern" to match against -- whether that be a plain scalar, a regex, an array or hash reference, a code reference, or whatever.

What is my in Perl?

my keyword in Perl declares the listed variable to be local to the enclosing block in which it is defined. The purpose of my is to define static scoping. This can be used to use the same variable name multiple times but with different values.


3 Answers

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

like image 91
Benedikt Köppel Avatar answered Sep 22 '22 02:09

Benedikt Köppel


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

like image 24
gaussblurinc Avatar answered Sep 24 '22 02:09

gaussblurinc


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
like image 34
matt freake Avatar answered Sep 25 '22 02:09

matt freake