YouTip LogoYouTip

Perl Variables

Variables are data stored in memory. Creating a variable allocates a space in memory. The interpreter determines the memory storage space based on the variable's type, so you can assign different data types to variables, such as integers, floating-point numbers, strings, etc. In the previous chapter, we introduced Perl's three basic data types: scalars, arrays, and hashes. Perl sets up a separate namespace for each variable type, so different types of variables can use the same name without worrying about conflicts. For example, $foo and @foo are two different variables. * * * ## Creating Variables Variables do not require explicit type declaration. After assigning a value to a variable, the interpreter automatically allocates the appropriate type space. Variables are assigned using the equals sign (=). > We can use the **use strict** statement in the program to enforce type declaration for all variables. The left side of the equals sign is the variable, and the right side is the value. Examples are as follows: $age = 25; # Integer $name = ""; # String $salary = 1445.50; # Floating-point number In the above code, 25, "", and 1445.50 are assigned to the _$age_, _$name_, and _$salary_ variables, respectively. Next, we will see the usage of arrays and hashes. * * * ## Scalar Variables A scalar is a single data unit. The data can be an integer, floating-point number, character, string, paragraph, etc. Simply put, it can be anything. Here is a simple application of scalars: ## Example #!/usr/bin/perl$age = 25; $name = ""; $salary = 1445.50; print"Age = $agen"; print"Name = $namen"; print"Salary = $salaryn"; The output of the above program is: Age = 25Name = Salary = 1445.5 * * * ## Array Variables Arrays are variables used to store an ordered list of scalar values. Arrays start with @. To access array elements, you can use the dollar sign ($) followed by the variable name and specify the index. An example is shown below: ## Example #!/usr/bin/perl@ages = (25, 30, 40); @names = ("google", "", "taobao"); print"$ages = $agesn"; print"$ages = $agesn"; print"$ages = $agesn"; print"$names = $namesn"; print"$names = $namesn"; print"$names = $namesn"; The output of the above program is: $ages = 25 $ages = 30 $ages = 40 $names = google $names = $names = taobao In the program, we used an escape character () before the $ sign to output the $ character. * * * ## Hash Variables A hash is a collection of **key/value** pairs. Hashes start with %. To access hash values, you can use the format **$ + {key}**: ## Example #!/usr/bin/perl%data = ('google', 45, '', 30, 'taobao', 40); print"$data{'google'} = $data{'google'}n"; print"$data{''} = $data{''}n"; print"$data{'taobao'} = $data{'taobao'}n"; The output of the above program is: $data{'google'} = 45 $data{''} = 30 $data{'taobao'} = 40 * * * ## Variable Context Context refers to the position where an expression is located. The context is determined by the type of variable on the left side of the equals sign. If the left side is a scalar, it is a scalar context; if it is a list, it is a list context. The Perl interpreter determines the type of the variable based on the context. An example is as follows: ## Example #!/usr/bin/perl@names = ('google', '', 'taobao'); @copy = @names; $size = @names; print"Names are: @copyn"; print"Number of names: $sizen"; The output of the above program is: Names are: google taobao Number of names: 3 In the code, @names is an array used in two different contexts. The first copies it to another array, so it outputs all elements of the array. The second assigns the array to a scalar, which returns the number of elements in the array. The following lists various different contexts: | No. | Context and Description | | --- | --- | | 1 | **Scalar βˆ’** Assigned to a scalar variable, evaluated on the right side in a scalar context. | | 2 | **List βˆ’** Assigned to an array or hash, evaluated on the right side in a list context. | | 3 | **Boolean βˆ’** Boolean context is a simple expression evaluation to check if it is true or false. | | 4 | **Void βˆ’** This context does not care about the return value, generally does not require a return value. | | 5 | **Interpolation βˆ’** This context only occurs within quotes. |
← Perl ConditionsPerl Intro β†’