Perl Language(I)Beginning of Perl
Perl Language(I)Beginning of Perl1. 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
---->
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 = "1";
print "first value = $array\n";
my ($first, $second, $third) = (1,2,3);
print "second Value = $second\n";
my @array = qw/first second third/;
print "third Value = @array\n";
my @array = (1...10);
my @array2 = (3, -1, @array, 13);
print "last Value = @array\n";
print "last Value = @array2\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 = "test";
print $array . "\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;
print @array2;
print "\n";
my @array3 = @array;
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]