Variables: Difference between revisions

m
Line 1,083:
 
=={{header|Perl}}==
 
Variables can be declared with <code>our</code>, <code>my</code>, or <code>local</code>, or they can be used without being declared at all; see [[scope modifiers]] for the differences. In any case, variables which haven't been assigned to have the undefined value by default. The undefined value acts just like <code>0</code> (if used as a number) or the empty string (if used as a string), except it can be distinguished from either of these with the <code>defined</code> function. Also, if warnings are enabled, perl will print a message like "Use of uninitialized value $foo in addition (+)" whenever you use the undefined value as a number or string.
In perl, variables are global by default and can be manipulated from anywhere in the program. Variables can be used without first being declared, unless the strict pragmatic directive is in effect:
 
<lang perl>sub dofruit {
$fruit='apple';
}
 
dofruit;
print "The fruit is $fruit";</lang>
Variables can be declared prior to use and may be prefixed with [[scope modifiers]] <code>our</code>, <code>my</code>, or <code>local</code>, or they can be used without being declared at all; see [[scope modifiers]] for the differences. In any case, variablesVariables which haven't been assigned to have the undefined value by default. The undefined value acts just like <code>0</code> (if used as a number) or the empty string (if used as a string), except it can be distinguished from either of these with the <code>defined</code> function. Also, ifIf warnings are enabled, perl will print a message like "Use of uninitialized value $foo in addition (+)" whenever you use the undefined value as a number or string.
 
Initialization and assignment are the same thing in Perl: just use the <code>=</code> operator. Note that the rvalue's context (scalar or list) is determined based on the lvalue.
Line 1,101 ⟶ 1,111:
 
There are two other sigils, but they behave quite unlike the others. A token of the form <code>&foo</code> refers to a subroutine named <code>foo</code>. In older versions of Perl, ampersands were necessary for calling user-defined subroutines, but since they no longer are, they have only a handful of obscure uses, like making references to named subroutines. Note that you can't assign to an ampersand-marked name. But you can assign to a typeglob, a kind of object represented with the notation <code>*var</code>. A typeglob <code>*foo</code> represents the symbol-table entry for all of the otherwise independent variables <code>$foo</code>, <code>@foo</code>, <code>%foo</code>, and <code>&foo</code>. Assigning a string <code>"bar"</code> to <code>*foo</code> makes these variables aliases for <code>$bar</code>, <code>@bar</code>, <code>%bar</code>, and <code>&bar</code> respectively. Alternatively, you can assign a reference to a typeglob, which creates an alias only for the variable of the appropriate type. In particular, you can say <code>*twiddle = sub {...}</code> to change the definition of the subroutine <code>&twiddle</code> without affecting <code>$twiddle</code> and friends.
 
If the strict pragmatic directive is in effect, then variables need explicit scope declaration, so should be prefixed with a my or our keyword depending on the required level of scope:
 
<lang perl>use strict;
our $fruit; # declare a variable as global
our $veg = "carrot"; # declare a global variable and define its value</lang>
 
=={{header|Perl 6}}==