perl-第一天(msn迁移过来)
时间:2007-02-21 来源:penny_kan
1、如果要在WEB页面访问PERL,需要在输出头加上如下行: print "Content-type: text/plain; charset=iso-8859-1\n\n"; 或 如果头是#!/bin/sh 则输出头为 # disable filename globbing
set -f #这里似乎可用中不用,但不清楚为什么 echo "Content-type: text/plain; charset=iso-8859-1"
echo 2、语句中换行,则显示为直接换行 3、命令与语句间空行没关系的,只要合法,以分号结束为一句语句的结束 4、双引号与单引号 双中可引用转义字符(interpolate),而单引号直接显示转义字符标识。 变量以$开头 *数值不需要用引号 5、Array:represents a lot of values; eg: my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed = ("camel", 42, 1.23); *变量:”$#array“ tells you the index of the last element of an array,这里也可以用来表示 print $mixed[$#mixed]; 这里返回1.23 *变量:“$#array + 1”to tell you how many items there are in an array;给出数组中的元素个数。 *数组分割:eg @animals[0,1]; # gives ("camel", "llama");
@animals[0..2]; # gives ("camel", "llama", "owl");
@animals[1..$#animals]; # gives all except the first element *其它用法: my @sorted = sort @animals;
my @backwards = reverse @numbers; *@ARGV 与@_变量的使用 @ARGV是为运算符<> 准备的,用于命令行,此数组的内容为打开过的文件handle;此运算符如果没有参数,则其默认变量为@ARGV上次打的handle。{这个说明还不算太明白} @_用于子function,表示参数,在定义一个FUNCTION过程中,未给定参数名称,调用它时又给定了参数,则@_[0...n],可以用于代表其值。
eg
sub max {
my $max = shift(@_);
foreach $foo (@_) {
$max = $foo if $max < $foo;
}
return $max;
}
$bestday = max($mon,$tue,$wed,$thu,$fri);
apple => "red",
banana => "yellow",
); 获取其中key与value的方法: You can get at lists of keys and values with keys() and values().
my @fruits = keys %fruit_colors; my @colors = values %fruit_colors;
此类型的特例:%ENV 包含些环境变量
一个包含三种类型的较为复杂的变量$,@,%
my $variables = {
scalar => {
description => "single item",
sigil => '$',
},
array => {
description => "ordered list of items",
sigil => '@',
},
hash => {
description => "key/value pairs",
sigil => '%',
},
};
7、Variable scoping
之前的变量定义前均有加my,这个不是必须的。但是如前面使用了use strict,则必须加。
关于变量就学习到这里,下一章节将学习条件语句
相关阅读 更多 +