| | 103 | Perl has three main variable types: scalars, arrays, and hashes. |
| | 104 | |
| | 105 | =over 4 |
| | 106 | |
| | 107 | =item Scalars |
| | 108 | |
| | 109 | A scalar represents a single value: |
| | 110 | |
| | 111 | my $animal = "camel"; |
| | 112 | my $answer = 42; |
| | 113 | |
| | 114 | Scalar values can be strings, integers or floating point numbers, and Perl |
| | 115 | will automatically convert between them as required. There is no need |
| | 116 | to pre-declare your variable types (though you can if you want -- see L<XXX>). |
| | 117 | |
| | 118 | Scalar values can be used in various ways: |
| | 119 | |
| | 120 | say $animal; |
| | 121 | say "The animal is $animal"; |
| | 122 | say "The square of $answer is ", $answer * $answer; |
| | 123 | |
| | 124 | There are a number of "magic" scalars with names that look like |
| | 125 | punctuation or line noise. These special variables are used for all |
| | 126 | kinds of purposes, and are documented in L<XXX>. The only one you |
| | 127 | need to know about for now is C<$_> which is the "default variable". |
| | 128 | It's used as the default argument to a number of functions in Perl, and |
| | 129 | it's set implicitly by certain looping constructs. |
| | 130 | |
| | 131 | say; # prints contents of $_ by default |
| | 132 | |
| | 133 | =item Arrays |
| | 134 | |
| | 135 | An array represents a list of values: |
| | 136 | |
| | 137 | my @animals = ("camel", "llama", "owl"); |
| | 138 | my @numbers = (23, 42, 69); |
| | 139 | my @mixed = ("camel", 42, 1.23); |
| | 140 | |
| | 141 | Arrays are zero-indexed. Here's how you get at elements in an array: |
| | 142 | |
| | 143 | say @animals[0]; # prints "camel" |
| | 144 | say @animals[1]; # prints "llama" |
| | 145 | |
| | 146 | The numeric index of the last element of an array can by found with C<@array.end>: |
| | 147 | |
| | 148 | say @animals[@animals.end]; # prints "owl" |
| | 149 | |
| | 150 | To find the number of elements in an array, use the C<elems> method: |
| | 151 | |
| | 152 | say @mixed.elems; # last element, prints 1.23 |
| | 153 | |
| | 154 | To get multiple values from an array: |
| | 155 | |
| | 156 | @animals[0,1]; # gives ("camel", "llama"); |
| | 157 | @animals[0..2]; # gives ("camel", "llama", "owl"); |
| | 158 | @animals[1..@animals.end]; # gives all except the first element |
| | 159 | |
| | 160 | This is called an "array slice". |
| | 161 | |
| | 162 | You can do various useful things to lists: |
| | 163 | |
| | 164 | my @sorted = @animals.sort; |
| | 165 | my @backwards = @numbers.reverse; |
| | 166 | |
| | 167 | =begin maintainer_notes |
| | 168 | |
| | 169 | I have no idea what the following is in p6... I'll deal with them when I get there. |
| | 170 | |
| | 171 | |
| | 172 | There are a couple of special arrays too, such as C<@ARGV> (the command |
| | 173 | line arguments to your script) and C<@_> (the arguments passed to a |
| | 174 | subroutine). These are documented in L<perlvar>. |
| | 175 | |
| | 176 | =end maintainer_notes |
| | 177 | |
| | 178 | =item Hashes |
| | 179 | |
| | 180 | A hash represents a set of key/value pairs: |
| | 181 | |
| | 182 | my %fruit_color = ("apple", "red", "banana", "yellow"); |
| | 183 | |
| | 184 | You can use whitespace and the C<< => >> operator to lay them out more |
| | 185 | nicely: |
| | 186 | |
| | 187 | my %fruit_color = ( |
| | 188 | apple => "red", |
| | 189 | banana => "yellow", |
| | 190 | ); |
| | 191 | |
| | 192 | To get at hash elements: |
| | 193 | |
| | 194 | %fruit_color{"apple"}; # gives "red" |
| | 195 | |
| | 196 | You can get at lists of keys and values with C<keys()> and |
| | 197 | C<values()>. |
| | 198 | |
| | 199 | my @fruits = %fruit_colors.keys; |
| | 200 | my @colors = %fruit_colors.values; |
| | 201 | |
| | 202 | Hashes have no particular internal order, though you can sort the keys |
| | 203 | and loop through them. |
| | 204 | |
| | 205 | =begin maintainer_notes |
| | 206 | |
| | 207 | Again, no idea here... will get to it eventually. |
| | 208 | |
| | 209 | Just like special scalars and arrays, there are also special hashes. |
| | 210 | The most well known of these is C<%ENV> which contains environment |
| | 211 | variables. Read all about it (and other special variables) in |
| | 212 | L<perlvar>. |
| | 213 | |
| | 214 | =end maintainer_notes |
| | 215 | |
| | 216 | =back |
| | 217 | |
| | 218 | Scalars, arrays and hashes are documented more fully in L<perldata>. |
| | 219 | |
| | 220 | More complex data types can be constructed using references, which allow |
| | 221 | you to build lists and hashes within lists and hashes. |
| | 222 | |
| | 223 | A reference is a scalar value and can refer to any other Perl data |
| | 224 | type. So by storing a reference as the value of an array or hash |
| | 225 | element, you can easily create lists and hashes within lists and |
| | 226 | hashes. The following example shows a 2 level hash of hash |
| | 227 | structure using anonymous hash references. |
| | 228 | |
| | 229 | my $variables = { |
| | 230 | scalar => { |
| | 231 | description => "single item", |
| | 232 | sigil => '$', |
| | 233 | }, |
| | 234 | array => { |
| | 235 | description => "ordered list of items", |
| | 236 | sigil => '@', |
| | 237 | }, |
| | 238 | hash => { |
| | 239 | description => "key/value pairs", |
| | 240 | sigil => '%', |
| | 241 | }, |
| | 242 | }; |
| | 243 | |
| | 244 | print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n"; |
| | 245 | |
| | 246 | Exhaustive information on the topic of references can be found in |
| | 247 | L<perlreftut>, L<perllol>, L<perlref> and L<perldsc>. |
| | 248 | |
| | 249 | |
| | 250 | |