博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Perl 学习手札之三: General syntax
阅读量:7015 次
发布时间:2019-06-28

本文共 1441 字,大约阅读时间需要 4 分钟。

一般性的语法:

#!/usr/bin/perl

use strict;
use warnings;
main(@ARGV);
sub main
{
    message("This is the template.pl exercise file from Perl 5 Essential Training.");
}
sub message
{
    my $m = shift or return;
    print("$m\n");
}
sub error
{
    my $e = shift || 'unkown error';
    print("$0: $e\n");
    exit 0;
}

1. write space in perl are: new line, space charactor andtab.

  write space可以放在任意位置, 解释器会自动忽略write space的作用。

2. semicolon:

  作为一行语句的结束判断符, 不需要加在大括号的后面;

3. comment:

#!/usr/bin/perl

# template.pl by Bill Weinman <http://bw.org/contact/>
# Copyright (c) 2010 The BearHeart Group, LLC
#
use strict;
use warnings;
main(@ARGV);
sub main
{

  my $n = shift ||5;

  my $r = factorial($n);

    message("$n factorial is $r");
}

#factorial(n)

#return the product of all integers up to and including n

# computed recursively

sub factorial{

  my $n = shift or return 0; #return 0 if no n

  if($n>1){ #only compute for n>1

    return $n * factorial($n-1); #recursion

  }else{

    return 1; #return 1 for n =1

  }

}

sub message
{
    my $m = shift or return;
    print("$m\n");
}
sub error
{
    my $e = shift || 'unkown error';
    print("$0: $e\n");
    exit 0;
}

我们添加了一个子函数叫做factorial, 并且对main函数进行了修正, 可以看到, 注释在此时的作用: 提示说明子函数的功能;

另: 还有其他的注释方式, 我们在这里暂时不涉及 ,如__END__来对所有的文件结尾部分进行注释, 和=begin开始到=end结束中间的代码块都是会被注释掉。

4. General syntax. 概述整个程序的各部分: shabang,use statement, subroutine

posted on
2011-12-05 00:06 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/hanleilei/archive/2011/12/05/2276280.html

你可能感兴趣的文章
Git学习的简单笔记
查看>>
9月5日队内互测总结
查看>>
测试的窘境
查看>>
[原创]隐藏用户名出现在Windows XP欢迎画面
查看>>
[SDOI2010]星际竞速——费用流
查看>>
C#开发串口总结,并提炼串口辅助类到公用类库中
查看>>
【个人笔记】《知了堂》MySQL中的数据类型
查看>>
Java “Unhandled exception type Exception”错误提示 (转)
查看>>
PHP源码之explode分析
查看>>
怪叔叔 一路走好 下辈子我们再一起玩KOF
查看>>
B.华华教月月做数学
查看>>
python 如何自动发送测试报告
查看>>
网络流24题4
查看>>
【第一组】第十一次例会纪要
查看>>
Hamming Weight的算法分析
查看>>
yii2 数据库操作详解(转载)
查看>>
JS函数
查看>>
c语言判断用户是否输入-非阻塞函数kbhit
查看>>
Hive基础
查看>>
(转)Sharepoint学习笔记—Debug--寻找 WSS_Logging下的ULSTraceLog
查看>>