Skip to content
Browse files

Subset of Pod cleanups in variables.pod that are wins with current Po…

…d parser
  • Loading branch information...
1 parent 7ea682f commit ac0c8299db81e1f12de39f60c64909624c1ad84f @japhb japhb committed Sep 4, 2012
Showing with 26 additions and 21 deletions.
  1. +26 −21 lib/variables.pod
View
47 lib/variables.pod
@@ -26,7 +26,7 @@ the documentation in L<List>.
Examples:
my $square = 9 ** 2;
- my @array = 1, 2, 3; # Array-variable with three elements
+ my @array = 1, 2, 3; # Array variable with three elements
my %hash = London => 'UK', Berlin => 'Germany';
There are two types of assignment, I<item assignment> and I<list assignment>.
@@ -70,11 +70,12 @@ depends on the $.
=end table
-=head2 *
+=head2 C<*>
Dynamic variables are looked up through the caller, not through the outer
scope. For example:
+=begin code
my $lexical = 1;
my $*dynamic1 = 10;
my $*dynamic2 = 100;
@@ -97,6 +98,7 @@ scope. For example:
# prints 1, 10, 101
say-all();
+=end code
The first time C<&say-all> is called, it prints "1, 10, 100" just as one would
expect. The second time though, it prints "1, 11, 101". This is because
@@ -107,11 +109,12 @@ C<$*dynamic1> isn't 11 anymore, but C<$*dynamic2> is still 101. This stems from
the fact that we declared a new dynamic variable C<$*dynamic1> in the block and
did not assign to the old variable as we did with C<$*dynamic2>.
-=head2 !
+=head2 C<!>
Attributes are variables that exists per instance of a class. They may be
directly accessed from within the class via C<!>:
+=begin code
class Point {
has $.x;
has $.y;
@@ -120,14 +123,15 @@ directly accessed from within the class via C<!>:
"($!x, $!y)"
}
}
+=end code
Note how the attributes are declared as C<$.x> and C<$.y> but are still
accessed via C<$!x> and C<$!y>. This is because in Perl 6 all attributes are
private and can be directly accessed within the class by using
C<$!attribute-name>. Perl 6 may automatically generate accessor methods for you
tough. For more details on objects, classes and their attributes see L<objects>.
-=head2 ?
+=head2 C<?>
Compile-time "constants" may be addressed via the C<?> twigil. They are known to
the compiler and may not be modified after being compiled in. A popular example
@@ -144,11 +148,12 @@ such constants.
For a list of those special variables see L<Compile-time "constants">.
-=head2 .
+=head2 C<.>
The C<.> twigil isn't really for variables at all. In fact, something along the
lines of
+=begin code
class Point {
has $.x;
has $.y;
@@ -157,6 +162,7 @@ lines of
"($.x, $.y)" # note that we use the . instead of ! this time
}
}
+=end code
just calls the methods C<x> and C<y> on C<self>, which are automatically
generated for you because you used the C<.> twigil as you declared your
@@ -166,12 +172,14 @@ don't want this to happen, use C<$!x> and C<$!y> instead.
The fact that the C<.> twigil just does a method call also implies that the
following is possible too.
+=begin code
class SaySomething {
method a() { say "a"; }
method b() { $.a; }
}
SaySomething.a; # prints "a"
+=end code
For more details on objects, classes and their attributes and methods see
L<objects>.
@@ -181,7 +189,7 @@ L<objects>.
The C<< < >> twigil is just an alias for C<< $/<...> >> where C<$/> is the match
variable. For more information on the match variable see L<$/>.
-=head2 ^
+=head2 C<^>
The C<^> twigil declares a formal positional parameter to blocks or subroutines.
Variables of the form C<$^variable> are a type of placeholder variables. They
@@ -208,15 +216,14 @@ have an explicit parameter list. This is true for normal blocks too.
sub say-it { say $^a; } # valid
sub say-it() { say $^a; } # invalid
-
{ say $^a; } # valid
-> $x, $y, $x { say $^a; } # invalid
Placeholder variables syntactically cannot have any type constraints. Be also
aware that one can not have placeholder variables with a single upper-case
letter. This is disallowed in favor of being to able to catch some Perl 5-isms.
-=head2 :
+=head2 C<:>
The C<:> twigil declares a formal named parameter to a block or subroutine.
Variables declared using this form are a type of placeholder variables too.
@@ -226,7 +233,7 @@ therefore not ordered using Unicode order, of course).
See L<^> for more details about placeholder variables.
-=head2 =
+=head2 C<=>
The C<=> twigil is used to access Pod variables. Every Pod block in the current
file can be accessed via a Pod object, such as C<$=data>, C<$=SYNOPSIS> or
@@ -247,7 +254,7 @@ data structure through C<$=pod>.
Note that all those C<$=someBlockName> support the C<Positional> and the
C<Associative> role.
-=head2 ~
+=head2 C<~>
The C<~> twigil is for referring to sublanguages (called slangs). The following
are useful:
@@ -290,7 +297,7 @@ There are three special variables that are available in every block:
=end table
-=head3 $_
+=head3 C<$_>
C<$_> is the topic variable. It is the default parameter for blocks that do
not have an explicit signature, so constructs like C<for @array { ... }>
@@ -312,7 +319,7 @@ Calling a method on C<$_> can be shortened by leaving off the variable name:
C<m/regex/> and C</regex/> regex matches and C<s/regex/subst/> substitutions
work on C<$_>.
-=head3 $/
+=head3 C<$/>
C<$/> is the match variable. It stores the result of each regex match, and
usually contains objects of type L<Match>.
@@ -329,7 +336,7 @@ Other match variables are aliases to elements of C<$/>:
$1 # same as $/[1]
$<named> # same as $/<named>
-=head3 $!
+=head3 C<$!>
C<$!> is the error variable. If a C<try> block or statement prefix catches
an exception, that exception is stored in C<$!>. If no exception was caught,
@@ -375,13 +382,11 @@ Other compile-time constants:
=head2 Other variables
- X<$*PROGRAM_NAME> Path to the current executable as it was typed in on the command line,
- or C<-e> if perl was invoked with the -e flag.
-
- X<$*PID> Process ID of the current process.
- X<$*OS> Which Operating System am I compiling under (e.g. linux)
- X<$*OSVER> Version of the current Operating System
- X<$*EXECUTABLE_NAME> The name of the perl executable that is currently running
+ X<$*PROGRAM_NAME> Path to the current executable as it was typed in on the
+ command line, or C<-e> if perl was invoked with the -e flag.
+ X<$*PID> Process ID of the current process.
+ X<$*OS> Which Operating System am I compiling under (e.g. linux)
+ X<$*OSVER> Version of the current Operating System
+ X<$*EXECUTABLE_NAME> The name of the perl executable that is currently running
=end pod
-

0 comments on commit ac0c829

Please sign in to comment.
Something went wrong with that request. Please try again.