The most common compile time variables are the following: $?FILE Being compile time they cannot be changed at runtime, however they are valuable in order to introspect the program. Rather, they set $_ inside the block to the caught exception.Īlso note that the same thread-safety issues apply to the use of $! as they do to $/.Īll compile time variables have a question mark as part of the twigil. If no exception was caught, $! is set to Nil. If a try block or statement prefix catches an exception, that exception is stored in $!. Finally, the List (42, "str") is list-assigned to $! variable It returns the assigned value 42, which in turn forms part of the List (42, "str") constructed by the comma operator that also has a higher precedence than the list assignment. The assignment expression involving the item assignment to the Scalar variable $num is thus evaluated first. The assignment to is a list assignment having a lower precedence than the item assignment to the Scalar variable $num. For instance, in the example below there is one chained assignment statement comprising two assignment operators. See for a more elaborate discussion of destructuring assignments in the context of variable declarations the section on declaring a list of variables with lexical or package scope.Ĭhained assignments are parsed having regard to the precedence of the assignment operators and, where applicable, their right associativity. The last two examples above are simple destructuring assignments that select the first item of the right-hand side list.
Accordingly, assignment to a Scalar container (scalar-context) triggers item assignment, unless the Scalar is explicitly put in list-context by surrounding parentheses ( ): As mentioned, item assignment is restricted to simple Scalar variables. In an assignment expression the context of the left-hand side determines whether an = means item or list assignment.
So without any list-delimiting parentheses (or other construct to hold the list's elements together), item assignment will only assign the first element of the specified list, and not the full list. In situations in which a comma-separated list of elements is assigned, these precedences should in particular be contrasted with that of the comma operator, which sits in between. The two types of assignment both use the equal sign = as their operator and are both right associative, but differ in operator precedence: item assignment has a higher precedence level (level: Item assignment) than list assignment (level: List prefix). For example, Array variables ( sigil) empty themselves on list assignment, and then iteratively copy all values from the right-hand side into themselves as elements. A list assignment leaves the choice of what the assignment operation entails to the variable on the left.
An assignment to anything other than a simple Scalar variable is parsed as a list assignment. There are two types of variable assignment, item assignment and list assignment.Īn item assignment copies a single value from the right-hand side into a Scalar variable on the left. # OUTPUT: «X::OutOfRange: Hash key out of range. Is: cherry, should be in (oranges bananas)»įor information on variables without sigils, see sigilless variables. The positional and associative-sigil also simplify assignment by flattening by default. The latter also tells the compiler where parentheses for calls can be omitted. The callable-sigil does the same for function calls. The positional-sigil and the associative-sigil provide type constraint that enforce base type subscripts required to know what methods to dispatch to. They provide a shortcut for the most common type constraints when declaring variables, and serve as markers for string interpolation.
Sigils provide a link between syntax, the type system and containers. The scalar-sigil $, the positional-sigil the associative-sigil % and the callable-sigil &. Variable names can start with or without a special character called a sigil, followed optionally by a second special character named twigil and then an identifier. Variable declarations or assignment of values may create a container on the fly. Variables are symbolic names for values or containers. Declaring a list of variables with lexical ( my ) or package ( our ) scopeĪrgument related filehandles: STDIN, STDOUT and STDERR