Categories
Posts in this category
- Introduction
- Strings, Arrays, Hashes;
- Types
- Basic Control Structures
- Subroutines and Signatures
- Objects and Classes
- Contexts
- Regexes (also called "rules")
- Junctions
- Comparing and Matching
- Containers and Values
- Where we are now - an update
- Changes to Perl 5 Operators
- Laziness
- Custom Operators
- The MAIN sub
- Twigils
- Enums
- Unicode
- Scoping
- Regexes strike back
- A grammar for (pseudo) XML
- Subset Types
- The State of the implementations
- Quoting and Parsing
- The Reduction Meta Operator
- The Cross Meta Operator
- Exceptions and control exceptions
- Common Perl 6 data processing idioms
- Currying
Thu, 16 Oct 2008
Containers and Values
Permanent link
NAME
"Perl 5 to 6" Lesson 10 - Containers and Values
LAST UPDATED
2015-02-26
Synopsis
my ($x, $y); $x := $y; $y = 4; say $x; # 4 if $x =:= $y { say '$x and $y are different names for the same thing' }
DESCRIPTION
Perl 6 distinguishes between containers, and values that can be stored in containers.
A normal scalar variable is a container, and can have some properties like type constraints, access constraints (for example it can be read only), and finally it can be aliased to other containers.
Putting a value into a container is called assignment, and aliasing two containers is called binding.
my @a = 1, 2, 3; my Int $x = 4; @a[0] := $x; # now @a[0] and $x are the same variable @a[0] = 'Foo'; # Error 'Type check failed'
Types like Int and Str are immutable, ie the objects of these types can't be changed; but you can still change the variables (the containers, that is) which hold these values:
my $a = 1; $a = 2; # no surprise here
Binding can also be done at compile time with the ::=
operator.
You can check if two things are bound together the =:=
comparison operator.
MOTIVATION
Exporting and importing subs, types and variables is done via aliasing. Instead of some hard-to-grasp typeglob aliasing magic, Perl 6 offers a simple operator.