Perl Language(I)Beginning of Perl

本文介绍Perl语言的基础知识,包括如何检查Perl版本、安装Perl文档、基本语法、变量定义、数组操作等核心内容。通过实例演示了标量、字符串、数组等数据类型的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值