华域联盟 perl perl常量、多维数组及变量的初始化的实例代码

perl常量、多维数组及变量的初始化的实例代码

例1:

复制代码 代码如下:

#!/usr/bin/perl

use strict;

use warnings;

my $test = "asdf";

print "${test}_test2\n";

#constant

use constant {

    AAA => "aaa",

    BBB=> "bbb",

    MIN_TOTAL => 12,

    SCORE_PASS => 90,

    SCORE_RED => 70,

};

print AAA;

print SCORE_PASS;

#two dimesion arrays

my @steps = (

      ["aaa", "aaavalue"],

      ["bbb","bbbvalue"],

      ["ccc","cccvalue"]
);

print "\n";

foreach my $i (0 .. $#steps){

  print "$steps[$i][0]:$steps[$i][1]\n";

}

代码2:

复制代码 代码如下:

my $a1;

print "$a1\n";

my $a2 = undef;

print "$a2\n";

if(!defined($a1)){print "a1 is not defined\n";}

if(!$a2){print "a2 is not defined\n";}

my $a3='';

if(!$a3){print "a3 is empty string\n";}

在定义变量时一定要初始化,或者在使用时判断是否defined,很多的时候还需要判断是否为空字符串。 特别是在使用getopt::long或cgi->query获得参数后要检测是否定义,如果么有定义考虑给予默认值。

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » perl常量、多维数组及变量的初始化的实例代码

转载请保留出处和原文链接:https://www.cnhackhy.com/76817.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部