home PYTHONJAVA
 

Perl scalar

A scalar is a simple unit of data.

A scalar can be an integer, a float, a character, a string, a paragraph, or a complete web page.

The following example demonstrates a simple application of scalars:

Instance

#!/usr/bin/perl $age = 20 ; # integer assignment $name = " welookups"; #string $salary= 130.50 ; # floats print "Age = $age\n"; print "Name = $name\n"; print "Salary = $salary\n";

Execute the above program, the output is:

Age = 20
Name = welookups
Salary = 130.5

digital scalar

A scalar is usually a number or a string. The following example demonstrates the use of different types of digital scalars:

Instance

#!/usr/bin/perl $integer= 200 ; $negative = -300; $floating= 200.340 ; $bigfloat = -1.2E-23; # octal 377 with 255 $octal = 0377 ; # hex FF, 255 in decimal $hexa = 0 xff; 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, the output is:

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 single quotes and double quotes:

Instance

#!/usr/bin/perl $var= " String Scalar - welookups Tutorial!"; $quote= ' I am in single quotes - $var'; $double= " I am in double quotes - $var"; $escape = " Escape character use-\tHello, World!< /span>"; print "var = $var\n"; print "quote = $quote\n"; print "double = $double\n"; print "escape = $escape\n";

Execute the above program, the output is:

var = string scalar -< /span> welookups Tutorial!
Quote = I am in single quotes - $var
double = I am in double quotes - string scalar - welookups Tutorial!
Escape = escaped characters -    Hello , World !

scalar operation

The following example demonstrates a simple operation of scalars:

Instance

#!/usr/bin/perl $str = " hello" . "world" ; # string connection $num = 5 + 10; #Additional number $mul= 4 * 5; # two-digit multiplication $mix = $str . $num; #Connect strings and numbers print "str = $str\n"; print "num = $num\n"; print "mix = $mix\n";

Execute the above program, the output is:

str = helloworld
Num = 15
Mix = helloworld15

Multiline string

We can use single quotes to output a multi-line string as follows:

Instance

#!/usr/bin/perl $string = ' welookups tutorial —— Learning is not only technology, but also a dream! '; print "$string\n";

Execute the above program, the output is:

welookups Tutorial
    —— Learn not only Technology is even more a dream! 

You can also use the syntax of "here" document to output multiple lines:

Instance

#!/usr/bin/perl print <<EOF; welookups tutorial —— Learning is not only technology, but also a dream! EOF

Execute the above program, the output is:

welookups Tutorial
    —— Learn not only Technology is even more a dream! 

Special characters

Below we will demonstrate the application of special characters in Perl, such as __FILE__, __LINE__, and __PACKAGE__ to indicate the file name, line number, and package name of the currently executing script.

Note: __ is two underscores, two underscores before and after __FILE__.

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

Instance

#!/usr/bin/perlprint "filename". __FILE__ . "\ n"; print "Line number" . __LINE__ ."\ n"; print "Package name" . __PACKAGE__ ."\ n"; # Unable to parse print "__FILE__ __LINE__ __PACKAGE__\n";

Execute the above program, the output is:

filename test.< /span>pl
line number 4
Package name main
__FILE__ __LINE__ __PACKAGE__

v string

A single integer starting with v followed by one or more periods separated by a period is treated as a string literal.

When you want to declare its numeric value directly for each character, the v-string provides a clearer way to construct such a string, unlike "\x{1}\x{14}\x{ 12c}\x{fa0}" This is not easy to understand, we can see the following example:

Instance

#!/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, the output is:

Wide character in print at test. pl line 7.
Smile = &#x263a;
Foo = foo
Martin = Martin

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.