`
sillycat
  • 浏览: 2489556 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Perl Language(II)

阅读更多
Perl Language(II)

4. Control Construct
4.1.2 true/false
#!/usr/bin/perl
print "input number=";
my $num = <STDIN>;
chomp($num); # remove the \n of this parameter

if($num < 5){
print "small";
}else{
print "big";
}

console output:
input number=18
big

4.1.4 parameter life time
#!/usr/bin/perl
my $num = 3;
{
    my $max = $num;
    print "inner value=".$max."\n";
}
print "outer value=".$max;

console output:
inner value=3
outer value=

compare number: <, >, >=, <=, ==, !=
compare string    : eq, lt, gt, le, ge, ne

4.2.2 unless
#!/usr/bin/perl
my $a = 5;
unless($a<3){     #unless vs if
print "$a<3 is false\n";
}
$a = 1;
if($a<3){
print "$a<3 is true";
}

console output:
5<3 is false
1<3 is true

4.3.4 else/elsif
if ($num == 1) {
   ...
} else {
   ...
}

if ($date eq '星期一') {
   ....
} elsif ($date eq '星期二') {
   ....
} elsif ($date eq '星期三') {
   ....
} else {
   print "你怎麼會有$date\n";
}

my $num = 1;
my $result = 1;    
while ($num <= 10) { 
      $result*=$num;
      $num = $num + 1;
}

4.3.2 until
my $num = 1;
my $result = 1;
until ($num > 10) {
      $result*=$num;
      $num = $num + 1;
}

console output:
num=2 result=1
num=3 result=2
num=4 result=6

4.4 for loop
my $result = 1;
for (my $num = 1; $num <= 10; $num = $num + 1) {
    $result *= $num;
}

for my $num (1...10) {   # $num from 1 to 10
    print $num;
}

equal to

for (1...10) {
    print $_;
}

equal to

print for (1...10);

4.4.4 element in array
#!/usr/bin/perl
my @array = qw/1 2 3 4 5/;
for (my $i = 0; $i <= $#array; $i++) {
    print $array[$i];
}
print "\n";

print foreach (@array);

console output:
12345
12345

#!/usr/bin/perl
my @array = qw/6 -4 8 12 -22 19 -8 42/;
my @positive;
for (@array) { 
    push @positive, ($_**2) if ($_ > 0);  #if element > 0, **2 and push to positive
}
print join "," , @positive;

console output:
36,64,144,361,1764

references:
http://easun.org/perl/perl-toc/
http://perl.apache.org/





分享到:
评论

相关推荐

    Perl Language Reference Manual

    perl 语言官方指导手册 英文版 希望喜欢 Perl Language Reference Manual

    Perl language

    Perl language textbook

    perl Language

    perl Language

    The Perl Programming Language

    介绍了Perl脚本语言的基本特征,给出Perl用于Internet和Web开发的程序实例,并和Phthon、Ruby进行了比较。

    Professional Perl Programming

    But Perl is far more than a scripting language for the web. Supported by a comprehensive standard library and hundreds of third-party libraries, Perl is an increasingly popular choice for application...

    Modern.Perl.4th.Ed

    You'll understand how the language fits together and discover the secrets used by the global Perl community. This beloved guide is now completely updated for Perl 5.22. When you have to solve a ...

    Perl.6.Essentials

    Description: Perl 6 Essentials is a sneak-preview of Perl 6, the widely-anticipated rewrite of the Perl programming language. Still in development, the Perl 6 project is a community-based effort to ...

    perl-5.8.8-38.el5.x86_64.rpm

    Perl is a high-level programming language with roots in C, sed, awk and shell scripting. Perl is good at handling processes and files, and is especially good at handling text. Perl's hallmarks are ...

    Learning Perl Intermediate Perl Mastering.Per Programming Perl 4本合集 英文 非扫描版

    Perl 一般被称为“实用报表提取语言”(Practical Extraction and Report Language),你也可能看到“perl”,所有的字母都是小写的。一般,“Perl”,有大写的 P,是指语言本身,而“perl”,小写的 p,是指程序...

    beginning perl for bioinformatics

    programming biologist who needs to solve very specific problems., Beginning Perl for Bioinformatics is designed to get you quickly over the Perl language barrier by approaching programming as an ...

    Perl.Template.Toolkit

    And because it has its own simple templating language, templates can be written and edited by people who don't know Perl. In short, the Template Toolkit combines the best features of its competitors,...

    Atom-atom-language-perl6,Atom/GithubPerl6语法突出显示支持.zip

    Atom-atom-language-perl6.zip,Atom/Github Perl 6 Syntax Highlighting Support »ö«Atom Perl 6支持-官方版!,atom是一个用web技术构建的开源文本编辑器。

    Perl脚本参考手册

    Perl是Practical Extraction and Report Language的缩写,它是由Larry Wall设计的,并由他不断更新和维护,用于在UNIX环境下编程。 .Perl具有高级语言(如C)的强大能力和灵活性。事实上,你将看到,它的许多特性是...

    perl 4 详细课程

    Perl 一般被称为“实用报表提取语言”(Practical Extraction and Report Language),虽然有时被称做“病态折中垃圾列表器” (Pathologically Eclectic Rubbish Lister)。它是术语,而不仅仅是简写,Perl 的创造者...

    Perl语言入门教程

    Perl 一般被称为“实用报表提取语言”(Practical Extraction and Report Language),你也可能看到“perl”,所有的字母都是小写的。一般,“Perl”,有大写的 P,是指语言本身,而“perl”,小写的 p,是指程序...

    perl的编程基础

    Extraction and Report Language的缩写,它是由Larry Wall设计的,并由他不断更新和维护,用于在UNIX环境下编程。 .Perl具有高级语言(如C)的强大能力和灵活性。事实上,你将看到,它的许多特性是从C语言中借用来...

    Intermediate Perl.pdf

    edition of Learning Perl. In the intervening years, Perl itself has grown substantially from a "cool" scripting language used primarily by Unix system administrators to a robust object-oriented ...

    Modern Perl 4th Editon 2016

    Since 1998, chromatic has ... You'll understand how the language fits together and discover the secrets used by the global Perl community. This beloved guide is now completely updated for Perl 5.22.

    Perl Data Language-开源

    PDL模块使标准perl能够精确存储和快速处理大型N维数据集,而大型N维数据集是科学计算的基础。

    Perl Best Practices

    "Damian's done what many thought impossible: show how to build large, maintainable Perl applications, while still letting Perl be the powerful, expressive language that programmers have loved for ...

Global site tag (gtag.js) - Google Analytics