示例1:
#!/usr/local/bin/perl -w
print "Hello,world!\n";
非常简单,真正的程序就一行(第二行)。第一行是每个Perl程序都需要写的。至于perl在你的系统的哪里,可以用which 或者whereis来查找。一般都在/usr/bin/perl或者/usr/local/bin/perl这两个地方。而-w是我自己的习惯。这是一个非常好的参数,他会指出你的程序哪里不规范需要注意的地方。建议你也加上。只有好处没有坏处^_^。第二句我想大家都能够看得懂。(\n是换行得意思)
示例2:
#!/usr/local/bin/perl -w
@lines = `perldoc -u -f atan2`;
foreach (@lines) {
s/\w<([^>]+)>/\U$1/g;
print;
}
这个例子就很有Perl得味道了。一大堆让你头晕得符号。不想仔细讲解这个例子,因为牵涉太多后面得知识,贴出来只是为了看看程序执行得结果:看看大家得眼睛是不是够厉害^_^。还有一点要指出得是第二行不是单引号,而是反引号,里面可以放入shell得命令!
先来讲讲perldoc这条命令:它是专门用来读出并显示Perl得文档、相关扩展以及实用工具。来看看perldoc -u -f atan2这条命令得执行结果:
bash-2.05$ perldoc -u -f atan2
=item atan2 Y,X
Returns the arctangent of Y/X in the range -PI to PI.
For the tangent operation, you may use the C
function, or use the familiar relation:
sub tan { sin($_[0]) / cos($_[0]) }
最后再来看看程序执行得结果:
bash-2.05$ ./ex1-3.plx
=item atan2 Y,X
Returns the arctangent of Y/X in the range -PI to PI.
For the tangent operation, you may use the POSIX::TAN()
function, or use the familiar relation:
sub tan { sin($_[0]) / cos($_[0]) }
看出区别了吧~~~,呵呵!结合程序先理解一下吧。
第一章的习题也就是让你感受一下这两个例子,因此就不打出来了。以后的习题我会打出来的。