5.7. Perl scalar

发布时间 : 2025-10-25 13:33:07 UTC      

Page Views: 9 views

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:

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“; 

Execute the above program, and the output is as follows:

Age = 20 Name = Runoob Salary = 130.5 

Digital scalar #

Scalars are usually a number or string, and the following examples demonstrate the use of different types of numeric scalars:

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"; 

Execute the above program, and the output is as follows:

integer = 200 negative = -300 floating = 200.34 bigfloat = -1.2e-23 octal = 255 hexa = 255 

String scalar #

The following example demonstrates the use of different types of string scalars, noting the difference between the use of single and double quotes:

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 "; 

Execute the above program, and the output is as follows:

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 #

The following example demonstrates the simple operation of scalars:

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; 

Execute the above program, and the output is as follows:

str = helloworld num = 15 mix = helloworld15 

Multiline string #

We can use single quotation marks to output multiline strings, as follows:

5.7.5. Example #

#/ Usr/bin/perl $string='Novice Tutorial—— Learning is not only about technology, but also about dreams; Print "$string n"; 

Execute the above program, and the output is as follows:

Novice Tutorial ——Learning is not only about technology, but also about dreams! 

You can also use the syntax format of “here” document to output multiple lines:

5.7.6. Example #

#!/usr/bin/perlprint<   

Execute the above program, and the output is as follows:

Novice Tutorial ——Learning is not only about technology, but also about dreams! 

Special character #

Below, we will demonstrate the application of special characters in Perl , such as \__FILE__ , \__LINE__ , and \__PACKAGE_\_ represent the file name, line number, and package name of the currently executing script.

Note: \_\_ is two underscores, \__ FILE_\_ represents two underscores before and after each.”

These special characters are separate tags and cannot be written in a string, for example:

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'; 

Execute the above program, and the output is as follows:

File name test.pl Line number 4 Package name main __ FILE____ LINE ___ PACKAGE__ 

V string #

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 "\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"; 

Execute the above program, and the output is as follows:

Wide character in print at test.pl line 7. smile = &#x263a; foo = foo martin = Martin 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。