perl学习笔记 - 快速入门!
来源:互联网 发布:java自学编程入门教程 编辑:程序博客网 时间:2024/05/21 09:37
参考文档
[1].a brief introduction and overview of Perl
[2].Perl syntax
[3].Perl 5教程
[4].Perl programming documentation
[5].http://www.ibm.com/developerworks/cn/linux/theme/perl/index.html
代码整理:
数据类型:标量、数组、哈希
逻辑控制:if,while,until,for
-:subroutine,regular
~/test/perl$vim perlintro.pl
输入以下内容:
#!/usr/bin/perl
use strict; #caught potential problems.
use warnings; #like command-line switch -w
print "*************Command-line************/n";
print "$0 @ARGV /n";
#
#Scalars
#
print "*************Scalars************/n";
my $animal = "camel";
my $answer = 42;
print "animal = ", $animal ,"/n";
print "answer = ", $answer, "/n";
#
#Array
#
print "*************Array************/n";
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed = ("camel", 32, 1.25);
print "animals[0] = ", $animals[0], "/n";
print "animals[1] = ", $animals[1], "/n";
#
# hash
#
print "*************hash************/n";
my %fruit_color = (
apple => "red",
banana => "yellow",
);
print 'fruit_color{"apple"} = ', $fruit_color{"apple"}, "/n";
my @fruits = keys %fruit_color;
my @colors = values %fruit_color;
print "*************foreach************/n";
foreach(@fruits) {
print "This fruits element is $_/n";
}
#
# if
#
print "*************if************/n";
my $fruit = $fruits[1];
if ($fruit eq "apple"){
print "fruit == apple/n";
} elsif ($fruit eq "banana") {
print "fruit == banana/n";
} else {
print "not apple or banana/n";
}
# the traditional way
my $zippy=1;
if ($zippy) {
print "Yow!/n;"
}
# the Perlish post-condition way
my $bananas=0;
print "Yow!/n" if $zippy;
print "We have no bananas/n" unless $bananas;
#
# while
#
print "*************while************/n";
print "/n";
my $count = 0;
while ($count < 10) {
print "count = ", $count, "/n";
$count += 1;
}
print "*************until************/n";
until ($count == 0) {
print "count = ", $count, "/n";
$count -= 1;
}
#
# for
#
print "*************for************/n";
my $max = 10;
my $i = 0;
for (; $i <= $max; $i++) {
print "i = " , $i, "/n";
}
#
# file
#
print "*************file************/n";
print "***read data from file: input.dat***/n";
open(my $in, "<", "input.dat") or die "Can't open input.dat:$!/n";
while (<$in>) { print "$_";}
close $in or die "$in:$!/n";
print "***write data to file: output.dat***/n";
open(my $out, ">", "output.dat") or die "Can't open output.dat:$!/n";
$i = 0;
for (;$i < 10; $i++) { print $out $i,"/n";}
close $out or die "$out:$!/n";
print "***append data to file: my.log.***/n";
open(my $log, ">>", "my.log") or die "Can't open my.log:$!/n";
print $log "append file!!!!/n";
close $log or die "$log:$!/n";
#
# Regular expressions
#
print "****** Regular expressions ******/n";
# Simple matching
# operator : // , =~
print "***Simple matching***/n";
foreach(@fruits) {
print "apple/n" if (/apple/);
}
if ($animal =~ /camel/) {print "animal is camel/n"; }
# Simple substitution
# operator : s///
print "*** Simple subsitution ***/n";
my $a = "hello aaa,aaa,aaa";
my $b = $a;
$b =~ s/aaa/bbb/;
my $c = $a;
$c =~ s/aaa/ccc/g;
print "a = $a , b = $b, c = $c/n";
my $email = 'dummy@dummy.com';
if ($email =~ /([^@]+)@(.+)/) {
print "Username is $1/n";
print "Hostname is $2/n";
}
#
# Writing subroutines
#
print "***************Writing subroutines******************/n";
sub logger {
print 'in sub logger @_ = ', @_, "/n";
my $logmessage = shift;
open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
print $logfile $logmessage;
}
logger("We have a logger subroutine!");
# return value
sub square {
my $num = shift;
my $result = $num * $num;
return $result;
}
my $sq = square(8);
print "squere(8) = $sq/n";
执行脚本
~/test/perl$chmod +w perlintro.pl
~/test/perl$./perlintro.pl
- perl学习笔记 - 快速入门!
- perl入门学习笔记
- perl入门学习笔记
- Learning Perl(Perl语言入门)学习笔记(1)
- perl语言入门学习笔记-qiu48com
- Perl 快速入门
- Perl快速入门
- Perl快速入门
- perl 快速入门
- Perl regex 快速入门
- XML学习笔记,快速入门
- perl快速学习记录
- perl语言入门笔记
- perl语言入门笔记
- perl入门笔记(一)
- perl入门笔记(二)
- perl入门笔记(三)
- perl入门笔记(五)
- 人活着就是个“累”想知道原因来看看!
- 表aae 每个项目编号下总价多少
- mysql5.1和5.5.7性能比较
- Portait and Landscape Layout
- myeclipse中直接复制项目不能直接运行的解决方案
- perl学习笔记 - 快速入门!
- new Objective-C Runtime Features
- XP操作系统 右键没有新建文本文件的解决方法
- mysql 《=,》=和between的性能比较
- srand函数资料收集
- Js-parentNode、parentElement,childNodes、children 它们有什么区别呢?
- WebOS
- [PostgreSQL]PostgreSQL后台进程启动相关代码的分析
- Adapter模式