Programming Perl 2

来源:互联网 发布:dbtg数据库 编辑:程序博客网 时间:2024/05/16 01:17
1。Variables

There are variable types corresponding to each of the three data types we mentioned. Each of these is introduced (grammatically speaking) by what we call a "funny character". Scalar variables are always named with an initial $, even when referring to a scalar that is part of an array or hash. It works a bit like the English word "the". Thus, we have:

Construct Meaning $days Simple scalar value $days $days[28] 29th element of array @days $days{'Feb'} "Feb" value from hash %days $#days Last index of array @days $days->[28]

29th element of array pointed to by reference $days

$#days-1 means the last second index

Entire arrays or array slices (and also slices of hashes) are named with @, which works much like the words "these" or "those":

Construct Meaning @days Same as ($days[0], $days[1],... $days[n]) @days[3, 4, 5] Same as ($days[3], $days[4], $days[5]) @days[3..5] Same as ($days[3], $days[4], $days[5]) @days{'Jan','Feb'} Same as ($days{'Jan'},$days{'Feb'})

Entire hashes are named by %:

Construct Meaning %days (Jan => 31, Feb => $leap ? 29 : 28, ...)