Perl Scalars
A scalar is a simple unit of data.
A scalar can be an integer, a floating-point number, a character, a string, a paragraph, or an entire web page.
The following example demonstrates the simple application of scalars:
Example
#!/usr/bin/perl
$age = 20; # Integer assignment
$name = "tutorialpro"; # String
$salary = 130.50; # Floating-point number
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
Executing the above program, the output is:
Age = 20
Name = tutorialpro
Salary = 130.5
Numeric Scalars
Scalars are typically numbers or strings. The following example demonstrates the use of different types of numeric scalars:
Example
#!/usr/bin/perl
$integer = 200;
$negative = -300;
$floating = 200.340;
$bigfloat = -1.2E-23;
# Octal 377, decimal 255
$octal = 0377;
# Hexadecimal FF, decimal 255
$hexa = 0xff;
print "integer = $integer\n";
print "negative = $negative\n";
print "floating = $floating\n";
print "bigfloat = $bigfloat\n";
print "octal = $octal\n";
print "hexa = $hexa\n";
Executing the above program, the output is:
integer = 200
negative = -300
floating = 200.34
bigfloat = -1.2e-23
octal = 255
hexa = 255
String Scalars
The following example demonstrates the use of different types of string scalars, noting the difference between single and double quotes:
Example
#!/usr/bin/perl
$var = "String Scalar - tutorialpro.org!";
$quote = 'I am inside single quotes - $var';
$double = "I am inside double quotes - $var";
$escape = "Escape character usage -\tHello, World!";
print "var = $var\n";
print "quote = $quote\n";
print "double = $double\n";
print "escape = $escape\n";
Executing the above program, the output is:
var = String Scalar - tutorialpro.org!
quote = I am inside single quotes - $var
double = I am inside double quotes - String Scalar - tutorialpro.org!
escape = Escape character usage - Hello, World!
Scalar Operations
The following example demonstrates simple operations with scalars:
Example
#!/usr/bin/perl
$str = "hello" . "world"; # String concatenation
$num = 5 + 10; # Addition of two numbers
$mul = 4 * 5; # Multiplication of two numbers
$mix = $str . $num; # Concatenation of string and number
print "str = $str\n";
print "num = $num\n";
print "mix = $mix\n";
Executing the above program, the output is:
str = helloworld
num = 15
mix = helloworld15
Multiline Strings
We can use single quotes to output multiline strings, as shown below:
Example
#!/usr/bin/perl
$string = '
tutorialpro.org
—— Learning is not only about technology, but also about dreams!
';
print "$string\n";
Executing the above program, the output is:
tutorialpro.org
—— Learning is not only about technology, but also about dreams!
You can also use the "here" document syntax to output multiline strings:
Example
#!/usr/bin/perl
print <<EOF;
tutorialpro.org
—— Learning is not only about technology, but also about dreams!
EOF
Executing the above program, the output is:
tutorialpro.org
—— Learning is not only about technology, but also about dreams!
Special Characters
The following demonstrates the application of special characters in Perl, such as __FILE__, __LINE__, and __PACKAGE__, which represent the current script's filename, line number, and package name, respectively.
Note: __
is two underscores, __FILE__
has two underscores before and after.
These special characters are standalone tokens and cannot be written inside strings, for example:
Example
#!/usr/bin/perl
print "Filename " . __FILE__ . "\n";
print "Line Number " . __LINE__ . "\n";
print "Package Name " . __PACKAGE__ . "\n";
# Cannot be parsed
print "__FILE__ __LINE__ __PACKAGE__\n";
Executing the above program, the output is:
Filename test.pl
Line Number 4
Package Name main
__FILE__ __LINE__ __PACKAGE__
v-Strings
A string starting with 'v' followed by one or more integers separated by dots is treated as a string literal.
When you want to declare the numeric value of each character directly, v-strings provide a clearer way to construct such strings compared to something like "\x{1}\x{14}\x{12c}\x{fa0}", which is less understandable. We can see the following example:
Example
#!/usr/bin/perl
$smile = v9786;
$foo = v102.111.111;
$martin = v77.97.114.116.105.110;
print "smile = $smile\n";
print "foo = $foo\n";
print "martin = $martin\n";
Executing the above program, the output is:
Wide character in print at test.pl line 7.
smile = ☺
foo = foo
martin = Martin