What is the difference between $this
, @that
, and %those
in Perl?
相关问题
- $ENV{$variable} in perl
- Extracting from a Union Type when some have identi
- Is it possible to pass command-line arguments to @
- Haskell: What is the differrence between `Num [a]
- Redirecting STDOUT and STDERR to a file, except fo
相关文章
- How do I get from a type to the TryParse method?
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Java Generics: How to specify a Class type for a g
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Lazy (ungreedy) matching multiple groups using reg
- How do I tell DBD::mysql where mysql.sock is?
People often try to tie sigils to variable types, but they are only loosely related. It's a topic we hit very hard in Learning Perl and Effective Perl Programming because it's much easier to understand Perl when you understand sigils.
Many people forget that variables and data are actually separate things. Variables can store data, but you don't need variables to use data.
The
$
denotes a single scalar value (not necessarily a scalar variable):The
@
denotes multiple values. That could be the array as a whole, a slice, or a dereference:The
%
denotes pairs (keys and values), which might be a hash variable or a dereference:Under Perl v5.20, the
%
can now denote a key/value slice or either a hash or array:You might want to look at the
perlintro
andperlsyn
documents in order to really get started with understanding Perl (i.e., Read The Flipping Manual). :-)That said:
$this
is a scalar, which can store a number (int or float), a string, or a reference (see below);@that
is an array, which can store an ordered list of scalars (see above). You can add a scalar to an array with thepush
orunshift
functions (seeperlfunc
), and you can use a parentheses-bounded comma-separated list of scalar literals or variables to create an array literal (i.e.,my @array = ($a, $b, 6, "seven");
)%those
is a hash, which is an associative array. Hashes have key-value pairs of entries, such that you can access the value of a hash by supplying its key. Hash literals can also be specified much like lists, except that every odd entry is a key and every even one is a value. You can also use a=>
character instead of a comma to separate a key and a value. (i.e.,my %ordinals = ("one" => "first", "two" => "second");
)Normally, when you pass arrays or hashes to subroutine calls, the individual lists are flattened into one long list. This is sometimes desirable, sometimes not. In the latter case, you can use references to pass a reference to an entire list as a single scalar argument. The syntax and semantics of references are tricky, though, and fall beyond the scope of this answer. If you want to check it out, though, see
perlref
.$this
is a scalar value, it holds 1 item likeapple
@that
is an array of values, it holds several like("apple", "orange", "pear")
%those
is a hash of values, it holds key value pairs like("apple" => "red", "orange" => "orange", "pear" => "yellow")
See perlintro for more on Perl variable types.
Perl's inventor was a linguist, and he sought to make Perl like a "natural language".
From this post:
Disambiguation by number, case and word order
Part of the reason a language can get away with certain local ambiguities is that other ambiguities are suppressed by various mechanisms. English uses number and word order, with vestiges of a case system in the pronouns: "The man looked at the men, and they looked back at him." It's perfectly clear in that sentence who is doing what to whom. Similarly, Perl has number markers on its nouns; that is, $dog is one pooch, and @dog is (potentially) many. So $ and @ are a little like "this" and "these" in English. [emphasis added]
A useful mnemonic for Perl sigils are:
Matt Trout wrote a great comment on blog.fogus.me about Perl sigils which I think is useful so have pasted below: