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 @
?
$ is for scalar variables(in your case a string variable.) @ is for arrays.
The former is a function call. The latter is a method call.
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.
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.
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$arr[0]
You can find more about Perl contexts here: http://www.perlmonks.org/?node_id=738558
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With