"aaa", BBB=> "bbb", MIN_T"/>

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


例1:


#!/usr/bin/perl
use strict;
use warnings;
my $test = "asdf";
print "${test}_test2n";
#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 "$a1n";
my $a2 = undef;
print "$a2n";
if(!defined($a1)){print "a1 is not definedn";}
if(!$a2){print "a2 is not definedn";}
my $a3='';
if(!$a3){print "a3 is empty stringn";}

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

perl的logwrapper使用实例代码
这里为大家举二个小例子,供朋友们学习参考。对任何的函数记录函数运行的时间。#!/usr/bin/perlusewarnings;usestrict;nostrict"refs";subtestLogToStd{print"Teststdout:n";

Perl中处理时间的几个函数
1、time函数:返回从1970年1月1日起累计的秒数使用:my$t=time();2、localtime函数:获取本地时区时间使用:my$t=localtime(time());3、gmtime函数:获取格林威治时

perl上下文介绍
my$para=@_这是标量上下文,$para得到的是@_中元素的个数。my($para)=@_这是列表上下文,$para得到的是@_中的元素值。具体是什么上下文是由等号左边的变量