A scalar is a simple data unit.
A scalar can be an integer, a floating-point number, a character, a string, a paragraph, or an entire webpage.
The following example demonstrates the basic use of scalars:
Example
#!/usr/bin/perl
$age = 20;
$name = "Tutorial";
$salary = 130.50;
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
Executing the above program gives the following output:
Age = 20
Name = Tutorial
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 = 0377;
$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 gives the following output:
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. Note the difference between using single quotes and double quotes:
Example
#!/usr/bin/perl
$var = "String Scalar - Tutorial!";
$quote = 'I am inside single quotes - $var';
$double = "I am inside double quotes - $var";
$escape = "Escape character usage -\t Hello, World!";
print "var = $var\n";
print "quote = $quote\n";
print "double = $double\n";
print "escape = $escape\n";
Executing the above program gives the following output:
var = String Scalar - Tutorial!
quote = I am inside single quotes - $var
double = I am inside double quotes - String Scalar - Tutorial!
escape = Escape character usage - Hello, World!
Scalar Operations
The following example demonstrates basic scalar operations:
Example
#!/usr/bin/perl
$str = "hello" . "world";
$num = 5 + 10;
$mul = 4 * 5;
$mix = $str . $num;
print "str = $str\n";
print "num = $num\n";
print "mix = $mix\n";
Executing the above program gives the following output:
str = helloworld
num = 15
mix = helloworld15
Multi-line Strings
We can use single quotes to output multi-line strings, as shown below:
Example
#!/usr/bin/perl
$string = 'Tutorial
ββ
Learning is not just about technology, but also about dreams!';
print "$string\n";
Executing the above program gives the following output:
Tutorial
ββ
Learning is not just about technology, but also about dreams!
You can also use the "here" document syntax to output multiple lines:
Example
#!/usr/bin/perl
print <<EOF;
Tutorial
ββ
Learning is not just about technology, but also about dreams!
EOF
Executing the above program gives the following output:
Tutorial
ββ
Learning is not just about technology, but also about dreams!
Special Characters
Below we will demonstrate the application of special characters in Perl, such as __FILE__, __LINE__, and __PACKAGE__, which represent the filename, line number, and package name of the currently executing script, respectively.
Note: __ is two underscores, with two underscores before and after FILE.
These special characters are standalone tokens and cannot be written inside a string, for example:
Example
#!/usr/bin/perl
print "Filename: " . __FILE__ . "\n";
print "Line number: " . __LINE__ . "\n";
print "Package name: " . __PACKAGE__ . "\n";
print "__FILE__ __LINE__ __PACKAGE__\n";
Executing the above program gives the following output:
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 for each character directly, v-strings provide a clearer way to construct such strings, unlike the less understandable "\x{1}\x{14}\x{12c}\x{fa0}". We can look at 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 gives the following output:
Wide character in print at test.pl line 7.
smile = βΊ
foo = foo
martin = Martin
YouTip