| 1 | =head1 NAME |
|---|
| 2 | |
|---|
| 3 | Perl6::Overview::Data - Data Types |
|---|
| 4 | |
|---|
| 5 | =head1 DESCRIPTION |
|---|
| 6 | |
|---|
| 7 | =head2 Scalar (C<$>) |
|---|
| 8 | |
|---|
| 9 | list $foo # $foo |
|---|
| 10 | +$foo # $foo as Num |
|---|
| 11 | ~$foo # $foo as Str |
|---|
| 12 | ?$foo # $foo as Bool |
|---|
| 13 | "$foo" # $foo as Str |
|---|
| 14 | item $foo # $foo |
|---|
| 15 | |
|---|
| 16 | =head3 Scalar -> Str |
|---|
| 17 | |
|---|
| 18 | .chomp # Returns chomped string with a .newline property giving the |
|---|
| 19 | newline removed |
|---|
| 20 | .chop |
|---|
| 21 | .ord |
|---|
| 22 | .lc |
|---|
| 23 | .lcfirst |
|---|
| 24 | .uc |
|---|
| 25 | .ucfirst |
|---|
| 26 | .chars # Length in characters |
|---|
| 27 | .graphs # Length in graphemes |
|---|
| 28 | .codes # Length in codepoints |
|---|
| 29 | .bytes # Length in bytes |
|---|
| 30 | .tr |
|---|
| 31 | .reverse |
|---|
| 32 | .split(/sep/) |
|---|
| 33 | .hex |
|---|
| 34 | .oct |
|---|
| 35 | .index |
|---|
| 36 | .rindex |
|---|
| 37 | .substr |
|---|
| 38 | .trans |
|---|
| 39 | .capitalize |
|---|
| 40 | |
|---|
| 41 | =head3 Scalar -> Num |
|---|
| 42 | |
|---|
| 43 | .abs |
|---|
| 44 | .cos |
|---|
| 45 | .exp |
|---|
| 46 | .int |
|---|
| 47 | .log |
|---|
| 48 | .rand |
|---|
| 49 | .sin |
|---|
| 50 | .sqrt |
|---|
| 51 | .chr |
|---|
| 52 | .floor |
|---|
| 53 | .ceil |
|---|
| 54 | |
|---|
| 55 | =head2 Array (C<@>) |
|---|
| 56 | |
|---|
| 57 | list @foo # List of elements |
|---|
| 58 | +@foo # @foo.elems |
|---|
| 59 | ~@foo # join ' ', @foo |
|---|
| 60 | ?@foo # ? @foo.elems |
|---|
| 61 | "@foo[]" # join ' ', @foo |
|---|
| 62 | @foo[i] # subscript |
|---|
| 63 | @foo[i, i] # slice |
|---|
| 64 | @foo[i; i] # multi-dimensional access |
|---|
| 65 | item @foo # \@foo |
|---|
| 66 | |
|---|
| 67 | =head3 Methods |
|---|
| 68 | |
|---|
| 69 | .elems # Number of elements |
|---|
| 70 | .join($sep) |
|---|
| 71 | .map:{ ... } |
|---|
| 72 | .grep:{ ... } |
|---|
| 73 | .pop |
|---|
| 74 | .push($elem) |
|---|
| 75 | .shift |
|---|
| 76 | .unshift($elem) |
|---|
| 77 | .reverse |
|---|
| 78 | .sort( { cond }, { cond }, ... ) |
|---|
| 79 | .kv # return index,value pairs |
|---|
| 80 | .pairs # return Pair objects instead |
|---|
| 81 | .uniq |
|---|
| 82 | |
|---|
| 83 | =head3 Shapes |
|---|
| 84 | |
|---|
| 85 | XXX - someone summarize shapes and multi-dimension syntax |
|---|
| 86 | |
|---|
| 87 | =head3 Constructors |
|---|
| 88 | |
|---|
| 89 | ARRAY = LIST |
|---|
| 90 | [ LIST ] # Array object |
|---|
| 91 | |
|---|
| 92 | =head3 Lists vs. Arrays |
|---|
| 93 | |
|---|
| 94 | (42) # this is neither a list nor an array, |
|---|
| 95 | # but a scalar (the Num 42) |
|---|
| 96 | (42,) # this is a one-element list |
|---|
| 97 | list 42 # this is a one-element list |
|---|
| 98 | (42,23) # this is a two-element list |
|---|
| 99 | |
|---|
| 100 | [] # this is a zero-element array |
|---|
| 101 | [42] # this is a one-element array |
|---|
| 102 | [42,] # this is a one-element array |
|---|
| 103 | [42,23] # this is a two-element array |
|---|
| 104 | |
|---|
| 105 | List construction (via the comma operator, &infix:<,>), does not create |
|---|
| 106 | containers, meaning that the elements are aliases: |
|---|
| 107 | |
|---|
| 108 | ($foo,$bar)[0] =:= $foo; # true |
|---|
| 109 | ($foo,$bar)[0] = $baz; # $foo changed to $baz |
|---|
| 110 | |
|---|
| 111 | This also means that |
|---|
| 112 | |
|---|
| 113 | (42,23)[0] = 17; # is a fatal error ("can't modify constant") |
|---|
| 114 | # Similarly, you can't shift(), pop(), unshift() etc. arrays created by |
|---|
| 115 | # the comma operator: |
|---|
| 116 | shift (42,23); # dies |
|---|
| 117 | |
|---|
| 118 | By contrast, Array construction by the [] operator, &circumfix:<[ ]>, |
|---|
| 119 | does create new containers: |
|---|
| 120 | |
|---|
| 121 | [$foo,$bar][0] =:= $foo; # false |
|---|
| 122 | [$foo,$bar][0] = $baz; # $foo not changed |
|---|
| 123 | |
|---|
| 124 | shift [42,23]; # lives (but the Array is discarded after |
|---|
| 125 | # the operation) |
|---|
| 126 | |
|---|
| 127 | my @array = (42,23); |
|---|
| 128 | shift @array; # works too, as assignment to an array |
|---|
| 129 | # implicitly creates new containers. |
|---|
| 130 | |
|---|
| 131 | (While in Perl 5 the distinction between lists and arrays was often |
|---|
| 132 | implicit, this distinction is made explicit Perl 6. Amongst other |
|---|
| 133 | reasons, this is because Array objects in scalar context is just itself, |
|---|
| 134 | it's no longer the parentheses which create list context, but a slurpy |
|---|
| 135 | array in some signature. In Perl 6, the comma operator creates lists, |
|---|
| 136 | parens are only for grouping: |
|---|
| 137 | |
|---|
| 138 | # Perl 5 |
|---|
| 139 | sub foo { (1,2,3) } |
|---|
| 140 | my $scalar = foo(); # 3 |
|---|
| 141 | my @array = foo(); # (1,2,3) |
|---|
| 142 | |
|---|
| 143 | # Perl 6 |
|---|
| 144 | sub foo { (1,2,3) } |
|---|
| 145 | my $scalar = foo(); # [1,2,3] |
|---|
| 146 | my @array = foo(); # (1,2,3) |
|---|
| 147 | |
|---|
| 148 | Also note that list context, i.e. the context supplied by a slurpy array in |
|---|
| 149 | a signature, automatically flattens arrays and hashes (but not scalars |
|---|
| 150 | containing array or hash objects): |
|---|
| 151 | |
|---|
| 152 | sub this_provides_list_context (*@things) { @things[0] } |
|---|
| 153 | my @foo = <a b c>; |
|---|
| 154 | my @bar = <d e f>; |
|---|
| 155 | say this_provides_list_context(@foo, @bar); |
|---|
| 156 | # "a", not the stringification of @foo (which would be "a b c"). |
|---|
| 157 | |
|---|
| 158 | The list constructing comma operator supplies this list context: |
|---|
| 159 | |
|---|
| 160 | sub *infix:<,> (*@things) {...} |
|---|
| 161 | |
|---|
| 162 | This also explains why (@foo, @bar) does not create an array containing two |
|---|
| 163 | elements, but a List containing C<@foo + @bar> items. (Like |
|---|
| 164 | C<@foo.concat(@bar)> in other languages.)) |
|---|
| 165 | |
|---|
| 166 | =head2 Hash (C<%>) |
|---|
| 167 | |
|---|
| 168 | list %foo # List of pairs |
|---|
| 169 | +%foo # +%foo.keys |
|---|
| 170 | ~%foo # ??? |
|---|
| 171 | ?%foo # ? %foo.keys |
|---|
| 172 | "%foo{}" # ??? |
|---|
| 173 | %foo{k} # subscript |
|---|
| 174 | %foo{k, k} # slice |
|---|
| 175 | %foo<> # %foo{<>} |
|---|
| 176 | %foo<<>> # %foo{<<>>} |
|---|
| 177 | %foo{k; k} # multi-dimensional access |
|---|
| 178 | item %foo # \%foo |
|---|
| 179 | |
|---|
| 180 | =head3 Methods |
|---|
| 181 | |
|---|
| 182 | .delete($key) |
|---|
| 183 | .exists($key) |
|---|
| 184 | .keys |
|---|
| 185 | .values |
|---|
| 186 | .kv # List of key, value, key, value, ... |
|---|
| 187 | .pairs # List of Pair having the same |
|---|
| 188 | |
|---|
| 189 | Shapes: |
|---|
| 190 | |
|---|
| 191 | XXX - help me |
|---|
| 192 | |
|---|
| 193 | Constructors: |
|---|
| 194 | |
|---|
| 195 | HASH = LIST |
|---|
| 196 | { PAIR, PAIR, ... } # anonymous object |
|---|
| 197 | hash( LIST ) |
|---|
| 198 | |
|---|
| 199 | =head2 Pair |
|---|
| 200 | |
|---|
| 201 | list $foo # $foo # object, so doesn't flatten to $k, $v |
|---|
| 202 | +$foo # ??? |
|---|
| 203 | ~$foo # ??? |
|---|
| 204 | ?$foo # ??? |
|---|
| 205 | "$foo" # ??? |
|---|
| 206 | item $foo # $foo |
|---|
| 207 | |
|---|
| 208 | =head3 Methods |
|---|
| 209 | |
|---|
| 210 | .kv # $key, $value |
|---|
| 211 | .key |
|---|
| 212 | .value |
|---|
| 213 | |
|---|
| 214 | =head3 Constructors (all anonymous) |
|---|
| 215 | |
|---|
| 216 | :key # key => 1 |
|---|
| 217 | :key{'value'} # key => 'value' |
|---|
| 218 | \__ thus: :key<>, :key<<>> |
|---|
| 219 | key => 'value' # key => 'value' (LHS auto-quoted) |
|---|
| 220 | |
|---|
| 221 | =head2 Things with blocks |
|---|
| 222 | |
|---|
| 223 | Named Anonymous See also |
|---|
| 224 | class Foo { } class { } oo |
|---|
| 225 | role Foo { } role { } oo |
|---|
| 226 | sub foo { } sub { }, or { } sub |
|---|
| 227 | method foo { } method { } oo, sub |
|---|
| 228 | macro foo { } sub |
|---|
| 229 | rule foo { } rule { } rules |
|---|
| 230 | grammar Foo { } grammar { } rules |
|---|
| 231 | package Foo { } package { } mod |
|---|
| 232 | module Foo { } module { } mod |
|---|
| 233 | |
|---|
| 234 | =head2 Variable names, containers, values, etc. |
|---|
| 235 | |
|---|
| 236 | A variable name is '$foo', '@foo', '%foo', etc. |
|---|
| 237 | |
|---|
| 238 | In the symbol table/the lexical pad, links from variable names to |
|---|
| 239 | their containers are stored. |
|---|
| 240 | |
|---|
| 241 | A variable with ... sigil always points to a ... container: |
|---|
| 242 | $ --> Scalar |
|---|
| 243 | @ --> Array |
|---|
| 244 | % --> Hash |
|---|
| 245 | & --> Scalar |
|---|
| 246 | ^ --> Scalar |
|---|
| 247 | |
|---|
| 248 | A scalar container holds a single cell. This cell can either be constant or |
|---|
| 249 | modifiable. Binding replaces the cell. |
|---|
| 250 | $a := $b; # $a's cell slot points to $b's cell. |
|---|
| 251 | $b := $c; # $b's cell slot points to $c's cell. |
|---|
| 252 | # $a's cell slot still points to what used to be $b's cell. |
|---|
| 253 | |
|---|
| 254 | Array and hash containers hold many cells. Again, binding replaces a cell. |
|---|
| 255 | @array[$idx] := $b; # @array's cell slot number $idx points to $b's |
|---|
| 256 | # cell. |
|---|
| 257 | $b := $c; # $b's cell slot points to $c's sell. |
|---|
| 258 | # @array's cell slot number $idx still points to |
|---|
| 259 | # what used to be $b's cell. |
|---|
| 260 | |
|---|
| 261 | Cells hold the actual values, for example the Num 3 or the Str "Pugs". |
|---|
| 262 | |
|---|
| 263 | To summarize: |
|---|
| 264 | '$foo' entry in the lexical pad points to... |
|---|
| 265 | the container of '$foo'. The container holds... |
|---|
| 266 | a cell, which in turn holds... |
|---|
| 267 | $foo's value. |
|---|
| 268 | |
|---|
| 269 | Examples: |
|---|
| 270 | |
|---|
| 271 | $foo = 42; |
|---|
| 272 | # '$foo' --> $foo's container --> cell 123 --> Num 42 |
|---|
| 273 | |
|---|
| 274 | $bar = \$foo; |
|---|
| 275 | # '$bar' --> $bar's container --> cell 456 --> Ref --> |
|---|
| 276 | --> $foo's container --> cell 123 --> Num 42 |
|---|
| 277 | |
|---|
| 278 | $grtz = 23; |
|---|
| 279 | @array = ($foo, $grtz) |
|---|
| 280 | # '@array' --> @array's container --> |
|---|
| 281 | # --> [ |
|---|
| 282 | # 0 --> cell 111 --> Num 42, |
|---|
| 283 | # 1 --> cell 222 --> Num 23, |
|---|
| 284 | # ] |
|---|
| 285 | # (Changing $foo or $grtz does not have an effect on @array yet.) |
|---|
| 286 | |
|---|
| 287 | @array[0] := $foo; |
|---|
| 288 | # '@array' --> @array's container --> |
|---|
| 289 | # --> [ |
|---|
| 290 | # 0 --> cell 123 --> Num 42, |
|---|
| 291 | # 1 --> cell 222 --> Num 23, |
|---|
| 292 | # ] |
|---|
| 293 | # (Changing $grtz still does not have an effect on @array. Changing $foo |
|---|
| 294 | # (by assignment) does have an effect on @array. Re-binding $foo to some |
|---|
| 295 | # other variable causes further assignments to $foo to have no effect on |
|---|
| 296 | # @array again.) |
|---|
| 297 | |
|---|
| 298 | C<=:=> tests whether the cell slots of two containers $a and $b point to the |
|---|
| 299 | same cell. |
|---|
| 300 | |
|---|
| 301 | $a = 42; |
|---|
| 302 | # '$a' --> $a's container --> cell 42 --> Num 42 |
|---|
| 303 | |
|---|
| 304 | $b = 42; |
|---|
| 305 | # '$b' --> $b's container --> cell 23 --> Num 42 |
|---|
| 306 | $a =:= $b; # false, of course |
|---|
| 307 | |
|---|
| 308 | $b := $a; |
|---|
| 309 | # '$b' --> $b's container --> cell 42 --> Num 42 |
|---|
| 310 | |
|---|
| 311 | C<===> tests whether the eternal values of two immutable cells are |
|---|
| 312 | identical; for two mutable cells, it evaluates the same way as C<=:=>. |
|---|
| 313 | |
|---|
| 314 | 42 === 42; # true |
|---|
| 315 | |
|---|
| 316 | my $a = 42; |
|---|
| 317 | my $b = 42; |
|---|
| 318 | $a =:= $b; # false, of course |
|---|
| 319 | $a === $b; # true |
|---|
| 320 | |
|---|
| 321 | (1,2) === (1,2); # true |
|---|
| 322 | [1,2] === [1,2]; # false |
|---|
| 323 | |
|---|
| 324 | C<eqv> tests whether current snapshots of the values of two mutable cells |
|---|
| 325 | are identical; for two immutable cells, it evaluates the same way as |
|---|
| 326 | C<===>. |
|---|
| 327 | |
|---|
| 328 | [1,2] eqv [1,2]; # true |
|---|
| 329 | |
|---|
| 330 | my $a = [1,2]; |
|---|
| 331 | my $b = [1,2]; |
|---|
| 332 | $a === $b; # false, of course |
|---|
| 333 | $a eqv $b; # true |
|---|
| 334 | |
|---|
| 335 | Method calls on arrays and hashes go to the container, whereas method |
|---|
| 336 | calls on scalars go to the cell. |
|---|
| 337 | |
|---|
| 338 | $dog.bark(); # method call dispatched to $dog's value, i.e. a Dog object. |
|---|
| 339 | @array.elems; # method call dispatched to @array's container, an Array object |
|---|
| 340 | |
|---|
| 341 | Using the .VAR postfix macro, you can force a method call to a scalar to |
|---|
| 342 | go to its container. An example of a method on a scalar is .exists, |
|---|
| 343 | which tests whether the container's cell slot is nonempty: |
|---|
| 344 | |
|---|
| 345 | sub foo ($a?) { $a.VAR.exists } |
|---|
| 346 | foo(); # false |
|---|
| 347 | foo(42); # true |
|---|
| 348 | |
|---|
| 349 | .exists on array and hash containers test whether an element of a given |
|---|
| 350 | index/name exists: |
|---|
| 351 | |
|---|
| 352 | my @array = (1,2,3); |
|---|
| 353 | @array.exists(1); # true (@array[1] is 2) |
|---|
| 354 | @array.exists(3); # false |
|---|
| 355 | |
|---|
| 356 | my %hash = (a => 1, b => 2); |
|---|
| 357 | %hash.exists("a"); # true (%hash<a> is 1) |
|---|
| 358 | %hash.exists("c"); # false) |
|---|
| 359 | |
|---|
| 360 | (Reading t/operators/binding/*, t/operators/identity.t, and |
|---|
| 361 | t/operators/value_equivalence.t is strongly recommended.) |
|---|