Sigil (computer programming)
In computer programming, a sigil (pronounced /ˈsɪdʒəl/ or /ˈsɪɡəl/; plural sigilia or sigils) is a symbol attached to a variable name, showing the variable's datatype or scope. In 1999 Philip Gwyn adopted the term "to mean the funny character at the front of a Perl variable".[1]
Contents |
[edit] Historical context
The use of sigils was popularized by the BASIC programming language. The best known example of a sigil in BASIC is the dollar sign ("$") appended to the names of all strings. Many BASIC dialects use other sigils to denote integers and floating point numbers, and sometimes other types as well.
Larry Wall adopted shell scripting's use of sigils for his scripting language Perl. However, as Perl is a dynamically typed language, the sigils specify not fine-grained data-types like strings and integers, but the more general categories of scalars (using a prefixed "$"), arrays (using "@"), hashes (using "%"), and subroutines (using "&"). Perl 6 introduces secondary sigils, or twigils, to indicate the scope of variables. Prominent examples of twigils in Perl 6 include "^", used with self-declared formal parameters ("placeholder variables"), and ".", used with object attribute accessors (i.e., instance variables).
[edit] Language comparison
In Unix shell scripting and in utilities such as Makefiles, the "$" is a unary operator that translates the name of a variable into its contents. While this may seem similar to a sigil, it is properly a unary operator for lexical indirection, similar to the * indirection operator for pointers in C, as noticeable from the fact that the dollar sign is omitted when assigning to a variable.
In the PHP language, which was largely inspired by Perl, "$" precedes any variable name. Names not prefixed by this are considered constants or functions.
In the MUMPS programming language, global variables and routines (procedures or subroutines) are prefixed by an uparrow (^), and the last identifier used can be referenced indirectly by an uparrow alone, this is referred to as the "naked" identifier.
In Ruby, ordinary variables lack sigils, but "$" is prefixed to global variables, "@" is prefixed to instance variables, and "@@" is prefixed to class variables. Ruby also uses (strictly conventional) suffix sigils: "?" indicates a predicate method returning (usually) a boolean value; and "!" indicates that the method is "not safe", often having a side effect.
In Windows PowerShell, which was partly inspired by Unix shells and Perl, variable names are prefixed by the "$" sigil.
In Transact-SQL, "@" precedes a local variable or parameter name. System variables (known as global variables) are distinguished by a "@@" prefix.
In mIRC script, identifiers have a "$" sigil, while all variables have a "%" prefixed (regardless of local or global variables or data type). Binary variables are prefixed by an "&".
Standard ML uses the prefix sigil "'" on a variable that refers to a type. If the sigil is doubled, it refers to a type for which equality is defined. The "'" character may also appear within or at the end of a variable, in which case it has no special meaning.
In XSLT, variables and parameters have a leading "$" sigil on use, although when defined in <xsl:param> or <xsl:variable> with the "name" attribute, the sigil is not included. Related to XSLT, XQuery uses the "$" sigil form both in definition and in use.
In Fortran, sigils are not used, but all variables starting with the letters I, J, K, L, M and N are integers by default. Fortran documentation refers to this as "implicit typing", though explicit typing is always available to allow any variable to be declared with any type. (The range I-N was chosen to be mnemonic with the first two characters of "integer").
In Common Lisp, special variables (with dynamic scope) are typically surrounded with * in what is dubbed the “earmuff convention”. While this is only convention, and not enforced, the language itself adopts the practice (e.g., *standard-output*). Similarly, some programmers surround constants with +.
In Scheme, by convention, the names of procedures that always return a boolean value usually end in "?". Likewise, the names of procedures that store values into parts of previously allocated Scheme objects (such as pairs, vectors, or strings) usually end in "!".
In CLIPS, scalar variables are prefixed with a "?" sigil while multifield (i.e. a 1-level list) variables are prefixed with "$?".
In C#, any variable names may be prefixed with "@". This is mainly used to allow the use of variable names that would otherwise conflict with keywords.[2] The "@" sigil can also be applied to string literals, which changes the way they are interpreted (character escapes are not used and strings can extend over multiple lines). The same is achieved in VB.Net by enclosing the name in square brackets, as in [end].[3]
In CycL variables are prefixed with a "?" sigil.[4] Similarly, constant names are prefixed with "#$" (pronounced "hash-dollar").[5]
In many dialects of BASIC, string variables are suffixed with "$", and integer variables and constants with "%". Other characters may be used to indicate floating-point numbers or specify precision.
Various programming languages including Haskell and Go treat identifiers beginning with a capital letter differently from identifiers beginning with a small letter, a practice related to the use of sigils.
[edit] Hungarian notation
Related to sigils is Hungarian notation, a convention for variable-naming that specifies variable type by attaching certain alphabetic prefixes to the variable name. Unlike sigils, however, Hungarian notation provides no information to the compiler; as such, explicit types must be redundantly specified for the variables (unless using a language with type inference). As most standard compilers do not enforce use of the prefixes, this permits omission and also makes code prone to confusion due to accidental erroneous use[6].
[edit] References
- ^ Mark Jason Dominus (November 10, 2003). "Re: sigil". PerlMonks. The Perl Foundation. http://www.perlmonks.org/index.pl/?node_id=306017. Retrieved 2009-09-12.
- ^ "C# Keywords". MSDN. http://msdn.microsoft.com/en-us/library/x53a06bb.aspx. Retrieved 2011-03-23.
- ^ "string (C# Reference)". MSDN. http://msdn.microsoft.com/en-us/library/362314fe.aspx. Retrieved 2011-03-23.
- ^ http://www.cyc.com/cycdoc/ref/cycl-syntax.html#variables
- ^ http://www.cyc.com/cycdoc/ref/cycl-syntax.html#constant%20names
- ^ Linux kernel coding style, by Linus Torvalds