设为首页 收藏本站
查看: 711|回复: 0

[经验分享] Perl Language(I)Beginning of Perl

[复制链接]

尚未签到

发表于 2017-5-17 06:01:15 | 显示全部楼层 |阅读模式
Perl Language(I)Beginning of Perl

1. About Perl
check my perl version
>perl -v
This is perl, v5.10.1 (*) built for i686-linux-gnu-thread-multi
(with 40 registered patches, see perl -V for more detail)

perl eclipse plugin
http://www.epic-ide.org/updates/
http://www.epic-ide.org/running_perl_scripts_within_eclipse/entry.htm

Create a perl project easyperl
Create a file with name Demo1.pl
[Run] ---->[Perl Local]

My first perl programme
>perl -e 'print "hello sillycat!\n"'
hello sillycat!

>vi ch1.pl
#!/usr/bin/perl
print "hello sillycat\n";

execute this programme
>perl ch1.pl

give the right to the file
>chmod a+x ch1.pl
>./ch1.pl

install the doc of perl
>sudo apt-get install perl-doc
>perldoc -f print

style of comments
#comments here
=begin
comments
=end

2. Scalar
2.1 numeric
>vi ch2_1.pl
#!/usr/bin/perl
$a = 1_300_300;
$b = 1.3e6;
print "$a\n";     # 1300300
print "$b\n";    #   13000000

2.2 string
>vi ch2_2.pl
#!/usr/bin/perl
$a = "perl";
$c = "在\tPerl\t中使用字串非常容易";
print "$a\n";
print "$c\n";

difference between '' and ""
$c = "在Perl中使用字串非常容易";
print '$c\n';
print "$c\n";

>vi ch2_3.pl
#!/usr/bin/perl
$a = 1357;
$b = 2468;
print $a+$b,"\n";
print $a.$b,"\n";
console output:
3825
13572468

>vi ch2_4.pl
#!/usr/bin/perl
use strict;
my $foo = 3;
$foo = 3 + (my $bar = 2);
print "$foo\n";
print "no message="."$bar\n";

>vi ch2_6.pl
#!/usr/bin/perl
#my $name;
my $name="sillycat";
if (defined($name)) {
    print $name."\n";
} else {
    print "it's undefined\n";
}

3. Array and List
>Demo1.pl in easyperl project
#!/usr/bin/perl
my @array;
$array[0] = "1";
print "first value = $array[0]\n";

my ($first, $second, $third) = (1,2,3);
print "second Value = $second\n";

my @array = qw/first second third/;
print "third Value = @array[2]\n";

my @array = (1...10);
my @array2 = (3, -1, @array, 13);
print "last Value = @array[9]\n";
print "last Value = @array2[1]\n";

console output:
first value = 1
second Value = 2
third Value = third
last Value = 10
last Value = -1

>Demo2.pl in easyperl project
#!/usr/bin/perl
my @array = qw{"first" "second" "third"};
$array[6] = "test";
print $array[6] . "\n";
print $#array . "\n"; # last index number of the array
$array[$#array + 1] = "last value";
print "Last Value:" . $array[$#array] . "\n"

console output:
test
6
Last Value:last value

3.3 push / pop
>Demo3.pl in easyperl project
#!/usr/bin/perl
my @array = qw{first second third};
print $#array . "\n";
push @array, 'fourth';
print $#array . "\n";
my $tmp = pop @array;
print $#array  . " tmp=" . $tmp . "\n";

my @array2 = qw{'test1','test2'};
push @array, @array2;
print 'number of array:' . @array . "\n";

console output:
2
3
2 tmp=fourth
number of array:4

3.4 shift/unshift
>demo2.pl in easyperl project
#!/usr/bin/perl
my @array = (1...10);
print @array;
print "\n";
shift @array;  # remove the first one
print @array;
print "\n";
unshift @array, 0;  # put 0 to the first one
print @array;  

console output:
12345678910
2345678910
02345678910

3.5 Part of the array
>demo4.pl in easyperl
#!/usr/bin/perl
my @array = (0...10);
my @array2 = @array[2...4];
print @array2;
print "\n";
my @array3 = @array[2...4,6];
print @array3;
print "\n";
my @array = (1...10);  
my $scale = @array + 4;  
print $scale;
print "\n";
my @scalar_array = ($scale,15);
print @scalar_array;

console output:
234
2346
14
1415

3.7.2 join
#!/usr/bin/perl
my @array = qw/-4 45 33 8 75/;
print join ',', @array;
print "\n";
print @array;

console output:
-4,45,33,8,75
-44533875

3.7.3 map
my @array = map { sqrt($_)*10 } qw/4 81/;
print join ',',@array;

console output:
20,90

3.7.4 grep
my @array = qw/-4 8 12 -22 19 -8 42/;  
my @positive = grep {$_ > 0} @array;  
print "$_\n" for @positive

console output:
8
12
19
42

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-378215-1-1.html 上篇帖子: 探密perl-解析perl源码(3) 下篇帖子: 探密perl-解析perl源码(2)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表