Sigil (computer programming) facts for kids
A sigil is a special symbol, like `$`, that you put in front of a variable's name in computer programming. It helps the computer know what kind of information the variable holds (its datatype) or where it can be used (its scope). Think of it like a label that tells you more about the variable!
The word "sigil" comes from the Latin word sigillum, which means "little sign". In older times, a sigil was a sign or image thought to have magical power. In programming, sigils help keep different parts of a program's "namespaces" (like different areas where names are used) separate and clear.
Contents
How Sigils Started
The idea of using sigils became popular with the BASIC programming language. The most famous example in BASIC is the dollar sign ("$
"). This symbol was added to the names of all strings, which are pieces of text. Because of this, programmers outside America often say "string" instead of "dollar" when they see the `$` symbol in code.
Many versions of BASIC also used other sigils, like "%
", to show if a variable held a whole number or a number with decimals.
Sigils in Perl and Raku
Later, a programmer named Larry Wall used the idea of sigils from shell scripting (a way to give commands to a computer) for his Perl programming language. In Perl, sigils don't just tell you if something is text or a number. They tell you about bigger categories of data:
- "
$
" is for scalars, which are single pieces of data (like one number or one word). - "
@
" is for arrays, which are lists of data. - "
%
" is for hashes, which are like lists where each item has a unique name. - "
&
" is for subroutines, which are blocks of code that do a specific task.
The Raku programming language, which is a newer version of Perl, even uses "secondary sigils" or twigils. These are extra symbols that show where a variable can be used. For example, "^
" is used for special variables that are declared automatically, and ".
" is used for parts of an object.
Sigils in Different Programming Languages
Many programming languages use sigils in their own ways:
- In CLIPS, single variables start with "
?
", while lists of variables start with "$?
". - In CycL, variables start with "
?
", and constant names (values that don't change) start with "#$
". - In Elixir, the "
~
" symbol is used, followed by a letter and then some text. For example,~r(foo)
creates a regular expression (a pattern for finding text). Programmers can even make their own sigils! - In mIRC script, identifiers (names for things) have a "
$
" sigil, and all variables have a "%
" prefix. - In MUMPS, "
$
" is used before built-in functions and special variables. "$$
" is for other types of functions. A caret ("^
") is used for routines (blocks of code) and global variables (data stored in a database). - In Objective-C, the "
@
" symbol is used for text that becomes an object, and also for keywords that define how classes (blueprints for objects) are structured. Inside classes, "-
" is for methods and variables that belong to an object, while "+
" is for things that belong to the class itself. - In PHP, which was inspired by Perl, "
$
" is used before every variable name. If a name doesn't have a "$
", it's usually a constant, a function, or a class name. - PILOT uses "
$
" for text variables, "#
" for whole number variables, and "*
" for labels in the program. - Ruby uses sigils for special types of variables:
* "$
" for global variables (can be used anywhere). * "@
" for instance variables (belong to a specific object). * "@@
" for class variables (belong to the class itself). Ruby also uses special symbols at the end of method names: "?
" means the method returns a true or false answer, and "!
" means the method might do something unexpected and needs careful handling.
- In Standard ML, "
'
" before a variable means it refers to a type. If it's "", it means a type where equality can be checked. - In Transact-SQL, "
@
" is for local variables or parameters. "@@
" is for system functions. "#
" and "##
" are used for temporary tables. - In Windows PowerShell, "
$
" is used before variable names, similar to Unix shells and Perl. - In XSLT and XQuery, "
$
" is used for variables and parameters. - In MEL, "
$
" is used before variable names to tell them apart from functions and commands.
Similar Ideas
Sometimes, other programming ideas look a bit like sigils:
Shell Scripting Variables
In Unix shell scripting, "$
" is used to get the value of a variable. For example, if you have a variable named `name` with the value "Alice", then `$name` would give you "Alice". This is a bit different from a sigil because you don't use the `$` when you first give a value to the variable. It's more like an operator that helps you get the content of the variable.
Naming Rules
Some languages have rules about how you name variables that are similar to sigils. For example, in Fortran, if a variable name starts with I, J, K, L, M, or N, the computer automatically knows it's a whole number. This is called "implicit typing."
Also, in languages like Prolog, Haskell, Ruby, and Go, names that start with a capital letter are treated differently from names that start with a small letter.
Stropping
Stropping is a way to use words that are normally special keywords in a language as variable names. For example, in C#, you can put "@
" in front of a variable name to use a word like "class" as a variable name, even though "class" is usually a keyword.
Hungarian Notation
Hungarian notation is a way of naming variables where you add a short prefix to the name to show what type of data it holds. For example, `strName` might mean "string name." However, unlike sigils, the computer doesn't actually use this prefix to understand the variable's type; it's just a helpful rule for programmers.
Literal Prefixes and Suffixes
Sometimes, symbols are added to numbers or text directly, not to variable names. These are called "literal affixes." For example, in C++, `0x10` means the number 16 in hexadecimal (base 16). The `0x` is a prefix that tells the computer how to read the number. Similarly, `r"C:\Windows"` in Python means a "raw string," where backslashes are treated as normal characters. These symbols change how the number or text itself is understood, not how a variable is named.
See also
In Spanish: Sigil para niños
- Delimiter
- Source code
- Token