Scalar is a simple data unit.
Scalars can be an integer, floating point, character, string, paragraph, or a complete web page.
The following example demonstrates the simple application of scalars: Execute the above program, and the output is as follows: Scalars are usually a number or string, and the following examples demonstrate the use of different types of numeric scalars: Execute the above program, and the output is as follows: The following example demonstrates the use of different types of string scalars, noting the difference between the use of single and double quotes: Execute the above program, and the output is as follows: The following example demonstrates the simple operation of scalars: Execute the above program, and the output is as follows: We can use single quotation marks to output multiline strings, as follows: Execute the above program, and the output is as follows: You can also use the syntax format of “here” document to output multiple lines: Execute the above program, and the output is as follows: Below, we will demonstrate the application of special characters in Note: These special characters are separate tags and cannot be written in a string, for example: Execute the above program, and the output is as follows: An integer that begins with v, followed by one or more integers separated byperiods, is treated as a string text. When you want to directly declare the numerical value of each character, thev-string provides a clearer way to construct such strings, unlike the difficult to understand method of Execute the above program, and the output is as follows: 5.7.1. Example #
#/usr/bin/perl$age=20;#Integer assignment$name=“Runoob”;#String $Salary=130.50#
floating point numberprint“age=$Age\\n”;print“Name=$Name\\n”;Print 'Salary'
=$salary \\n“;
Age = 20
Name = Runoob
Salary = 130.5
Digital scalar #
5.7.2. Example #
#!/usr/bin/perl$integer=200;$negative= -300;$floating=200.340;$bigfloat=
-1.2E-23;# Octal 377 , Decimal is 255$octal=0377;# hexadecimal is FF, Decimal is
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";
integer = 200
negative = -300
floating = 200.34
bigfloat = -1.2e-23
octal = 255
hexa = 255
String scalar #
5.7.3. Example #
#/ Usr/bin/perl $var="String Scalar - Beginner Tutorial!"$ Quote='I am within single quotes-
$var '$ Double="I am within double quotes - $var"$ Escape="Escape characters using - tHello,
World! ; print "var=$var n
=$double n "; print" escape=$escape n ";
Var=string scalar - rookie tutorial!
Quote=I am within single quotes - $var
Double=I am inside double quotes - string scalar - rookie tutorial!
Escape=Use escape characters - Hello, World!
Scalar operation #
5.7.4. Example #
#/ Usr/bin/perl $str="hello" 'world'# String connection $num=5+10#
Add two numbers $mul=4 * 5# Multiplying two numbers by $mix=$str$ Num# Connect string and number print "str
=$str n "; print" num=$num n "; print" mix=$mix n;
str = helloworld
num = 15
mix = helloworld15
Multiline string #
5.7.5. Example #
#/ Usr/bin/perl $string='Novice Tutorial——
Learning is not only about technology, but also about dreams; Print "$string n";
Novice Tutorial
——Learning is not only about technology, but also about dreams!
5.7.6. Example #
#!/usr/bin/perlprint<<EOF;
Novice Tutorial - Learning not only skills, but also dreams!
Novice Tutorial
——Learning is not only about technology, but also about dreams!
Special character #
Perl
, such as
\__FILE__
,
\__LINE__
, and
\__PACKAGE_\_
represent the file name, line number, and package name of the currently executing script.
\_\_
is two underscores,
\__
FILE_\_
represents two underscores before and after each.” 5.7.7. Example #
#/ Usr/bin/perlprint "file name"\__ FILE_ _ N; Print "line number"\__ LINE N;
Print 'Package Name'\__ PACKAGE_ _ N#
Unable to parse print ' _ FILE _ _ LINE _ _ _ PACKAGE_ _ n';
File name test.pl
Line number 4
Package name main
__ FILE____ LINE ___ PACKAGE__
V string #
"\x{1}\x{14}\x{12c}\x{fa0}"
, which can be seen in the following example: 5.7.8. 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";
Wide character in print at test.pl line 7.
smile = ☺
foo = foo
martin = Martin