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

Perl Language(I)Beginning of Perl

阅读更多
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/

分享到:
评论

相关推荐

    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 ...

    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.Debugged

    coach of the USA Programming Team, I find the most difficult thing to teach is debugging. This is the first text I've even heard of that attacks the problem. It does a fine job. Please encourage these...

    Beginning Perl Programming.pdf

    Get started with Perl 5 and learn the important core concepts of Perl programming, such as variables, flow control, expressions, and I/O. Additionally, this book covers pattern matching and shows that...

    Apress.Beginning.Perl.3rd.Edition.Apr.2010

    Originally touted as the duct tape of the Internet, Perl has since evolved into a multipurpose, multiplatform language present absolutely everywhere: heavy-duty web applications, the cloud, systems ...

    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 ...

    Beginning Perl

    Making use of online Perl resources like CPAN First principles in programming and the Perl syntax Working with files and databases Writing web pages in Perl Using Perl as an object-oriented language

    Perl Debugged.pdf

    Table of Content.........................................................................................................................i Copyright......................................................

    Beginning Linux Programming

    A bit of a programming linguist, he has programmed in various assemblers, a rather neat proprietary telecommunications language called SL-1, some FORTRAN, Pascal, Perl, SQL, and smidgeons of Python ...

    Python 2.1 Bible .pdf

    considered by many to be the language of choice for beginning programmers. Instead of outgrowing the language, however, experienced developers enjoy lower maintenance costs without missing out on any ...

    freemarker总结

    JAVA模版引擎Freemarker常用标签(一) 1. if指令 这是一个典型的分支控制指令,该指令的作用完全类似于Java语言中的if,if指令的语法格式如下: <#if condition>... <#elseif condition>... <#elseif condition>......

    2009 达内Unix学习笔记

    mv [-f] [-i] f1 ... fn d1 mv [-f] [-i] d1 d2 mv 源文件名 目标文件名 若目标文件名还没有,则是源文件重命名为目标文件;若目标文件已存在,则源文件覆盖目标文件。 mv 源文件名 目标目录 移动文件 mv 源目录...

    DebuggingWithGDB 6.8-2008

    Table of Contents Summary of gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Free Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

Global site tag (gtag.js) - Google Analytics