上一节:
下一节:

  Perl 标量

  • 标量

    标量是数据的单个单位。该数据可能是整数,浮点数,字符,字符串,段落或整个网页。
    这是一个使用标量变量的简单示例-
    
    $age = 25;             # An integer assignment
    $name = "John Paul";   # A string 
    $salary = 1445.50;     # A floating point
    
    print "Age = $age\n";
    print "Name = $name\n";
    print "Salary = $salary\n";
    
    尝试一下
    这将产生以下结果-
    
    Age = 25
    Name = John Paul
    Salary = 1445.5
    
  • 数字标量

    标量通常是数字或字符串。以下示例演示了各种类型的数字标量的用法-
    
    $integer = 200;
    $negative = -300;
    $floating = 200.340;
    $bigfloat = -1.2E-23;
    
    # 377 octal, same as 255 decimal
    $octal = 0377;
    
    # FF hex, also 255 decimal
    $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
    
  • 字符串标量

    以下示例演示了各种类型的字符串标量的用法。注意单引号字符串和双引号字符串之间的区别-
    
    $var = "This is string scalar!";
    $quote = 'I m inside single quote - $var';
    $double = "This is inside single quote - $var";
    
    $escape = "This example of escape -\tHello, World!";
    
    print "var = $var\n";
    print "quote = $quote\n";
    print "double = $double\n";
    print "escape = $escape\n";
    
    尝试一下
    这将产生以下结果-
    
    var = This is string scalar!
    quote = I m inside single quote - $var
    double = This is inside single quote - This is string scalar!
    escape = This example of escape -       Hello, World
    
  • 标量运算

    您将在单独的章节中看到Perl中可用的各种运算符的详细信息,但是在这里,我们将列出一些数字和字符串运算。
    
    $str = "hello" . "world";       # Concatenates strings.
    $num = 5 + 10;                  # adds two numbers.
    $mul = 4 * 5;                   # multiplies two numbers.
    $mix = $str . $num;             # concatenates string and number.
    
    print "str = $str\n";
    print "num = $num\n";
    print "mul = $mul\n";
    print "mix = $mix\n";
    
    尝试一下
    这将产生以下结果-
    
    str = helloworld
    num = 15
    mul = 20
    mix = helloworld15
    
  • 多行字符串

    如果要在程序中引入多行字符串,可以使用以下标准单引号-
    
    $string = 'This is
    a multiline
    string';
    
    print "$string\n";
    
    尝试一下
    这将产生以下结果-
    
    This is
    a multiline
    string
    
    您还可以使用“Here”文档语法来存储或打印多行,如下所示:
    
    print <<EOF;
    This is
    a multiline
    string
    EOF
    
    尝试一下
    这也将产生相同的结果-
    
    This is
    a multiline
    string
    
  • V型字符串

    v1.20.300.4000形式的文字被解析为由具有指定序数的字符组成的字符串。这种形式称为v弦。v字符串提供了一种替代的且更易读的方式来构造字符串,而不是使用可读性较差的内插形式“\x{1}\x{14}\x{12c}\x{fa0}”。它们是任何以av开头且后接一个或多个点分隔元素的文字。例如-
    
    $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";
    
    尝试一下
    这也将产生相同的结果-
    
    smile = ☺
    foo = foo
    martin = Martin
    Wide character in print at main.pl line 7.
    
  • 特殊常量

    到目前为止,您必须对字符串标量及其串联和插值运算有一定的了解。因此,让我告诉您三个特殊的常量__FILE____LINE____PACKAGE__代表程序中该点的当前文件名,行号和程序包名称。
    它们只能用作单独的标记,而不会内插到字符串中。检查以下示例-
    
    print "File name ". __FILE__ . "\n";
    print "Line Number " . __LINE__ ."\n";
    print "Package " . __PACKAGE__ ."\n";
    
    # they can not be interpolated
    print "__FILE__ __LINE__ __PACKAGE__\n";
    
    尝试一下
    这将产生以下结果-
    
    File name Tmp5f58a048a29b11599643720.pl
    Line Number 2
    Package main
    __FILE__ __LINE__ __PACKAGE__
    
上一节:
下一节: