`
happmaoo
  • 浏览: 4460889 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

perl 新得

阅读更多

1. "@"当作数组缘于array首字母哦,标量呢?"$"当然是scalar了。
2. "=>"是comma(,)的同义词哦,所以不要以为"=>"只用于hash!
@array = (1,2=>3=>4=>5);
print "@array"#amazing,output:1 2 3 4 5
3. 要输出一个数组,那么如何分隔开数组元素呢?
print join("\n",@array);#老土!
print "@array";#默认元素之间为空格,如果想用其他分隔符,修改$"(刀了和一个双引号);
4 可以象打开文件一样地打开管道。
open (SESAME,"| output-pipe-command");
open (SESAME,"input-pipe-command | ");
5. 指数操作符"**",比如2**4=16,哈哈,当然你可以打一连串*,不知道是什么玩意了。
6. repeat operator "x",比较好玩
print "12"x 3;# out:121212
print "-" x $screenwidth;
print "\a"x 100000;#bt,你的电脑费了!
7. open (IN,"-");#猜猜是什么,竟然是标准输入,标准输出是什么?plz tell me if u know.
8. 来几个变态的赋值操作
$kissyou x=1000;#omg! kiss you a thousand time.
$begger ||= "100 dollar";#if the $begger have nothing(undefined),then give her $100 if I have.(But I have nothing too!Q)
9. 先增还是后增? 如果$i++和++$i对你一样,那么后者(pre-increment)效率高些。why? this is not clear to me .酱紫!
10.如何去的文件信息?用stat函数啊!
$file_size = (stat(filehandle or filename))[7];#第8项为字节数。注意:stat函数外层要加括号,告诉人家他是list context,ok?
11. 来个自己写的递归查找文件
use Cwd;
use strict;
use File::Copy;
mkdir("c:/temp/");
my $trg="c:/temp/";

my $dir = cwd;
my @files = glob("*");
fkpic(\@files);
sub fkpic{
my $files = shift;
foreach my $file(@$files){
if(-d $file){
my $cwdir = cwd;
chdir($cwdir . "/". $file);
my @allfiles = glob("*");
fkpic(\@allfiles);
chdir($cwdir);
}
if(-f $file){
if($file =~ /.*?jpg/ and (stat($file))[7] > 30000)#把大于30k的jpg文件移到目标文件夹
{copy ($file,$trg);}
}
}
}
12.what is truth? Perl 判官如何分别对错?
四条法律:
(1)Any string is true except for "" and "0";
(2)Any number is true exceppt for 0.
(3)Any reference is true.
(4)Any undefined value is false.
Make a test:
<1> 10-10 #false because 10-10 is 0
<2> 0.00 #false because 0.00 became 0
<3> "0.00" #true! ft! haha, because "0.00" is not empty neither "0"
<4>"0.00" + 0#false! puke! ceforced by the "+" ,it becames 0;
<5>\$a #true! even if $a is flase.
<6>undef() #false!a function return a underfined value ,so it is false.
13. foreach 中的循环变量是元素的reference 而不是copy,所以,对它进行改动,其实就是对所循环的list作了改动。
14. 正则匹配中的left-most原则高于longest原则!
$ = "fred xxxxx jelinek";
s/x*//;#so what will be $_? in fact it will not change! because left-most RE match "" before "fred";
15. Geedy 匹配 and non-greedy .这个简单,默认的是greedy,但是我们加上一个"?"就变成最小匹配了。
$_ = "hfjiang xxx mtlabmtlabmtlab";
s/^hfjiang.*mtlab/sound/;#$_="sound";
s/^hfjiang.*?mtlab/sound/;#$_="soundmtlabmtlab";
16. backreference 的用法。
#在同一个模式里面的backreference 用\1,\2等。
m/<(.*?)>(.*?)<\/\1>/;#匹配如<B>Bold</B> 这样的标签
#模式外面的用$1,$2等。注意s///的两个模式之间也算模式外面。
s/(\w+)\s+(\w+)/$2 $1/;#调换两个词的位置
注意
\1 和$1 ,\1...\9默认都是匹配,但是\10,\11要看情况,\i(i>=10)表示如果最近的成功的匹配中有第i个匹配,否则。。。。
17. $' $` $& $+ 在匹配中都是很有用的。
$+ returns whatever the last braket matched.
$& returns the entire matched string.
$` returns everything before the matched string.
$' returns everything after the matched string.

##########################
#example
$_ = "xxxxabbbcczzzz";
m/(a((b+)(c+)))/g;
print <<EOP
\$1 = $1
\$2 = $2
\$3 = $3
\$4 = $4
\$+ = $+
\$& = $&
\$` = $`
\$\' = $'
EOP
;
#########################
output:
$1 = abbbcc
$2 = bbbcc
$3 = bbb
$4 = cc
$+ = cc
$& = abbbcc
$` = xxxx
$' = zzzz
##########################
我们可以总结出几点通过这个例子。
(1)$1,$2..的顺序是从左括号从左到右数起,到对应的右括号为止;
(2)几个特殊变量会挺有用的,真的!不信在下面将给你演示一个$`的例子。
(3)上面例子中输出时用了"here document"格式,帅吧?后面给你讲。
18. split模式中括号的用法.
(1)如果在返回的list中不想要分隔符,那么在分隔模式中不加括号。
(2)如果在返回的list中想要分隔符,那么在分隔模式中加括号。
尝试下面例子的两种情况
#######################################################################
#example
@arr = split /[-,]/, "1-10,20"; # or @arr = split /([-,])/, "1-10,20";
$" = "\n";
print "@arr";
########################################################################
那么有个问题:如果必须用括号,但是还不想要分隔符,咋办?凉拌!下面在RE extension中给您分解!
19.RE extension的用法
RE ext 的语法呢很简单,就是括号里面第一个字符是个问号(?xxx),这东东在早先的版本里面会被认为是语法错误,现在的作用可
大着哩,而且还在不断扩充哦(I think)!目前支持的语法有几个。
(1) (?#text) 这玩艺个人觉得还不如在RE的pattern中用/x开关,直接用"#"加注释的好。
(2)(?:...) 这个好玩,可以用来解决split中必须用括号但不想要分隔符的问题。
#example
$_ = "xxxx a yyyy b zzzz c mmmm";
split (/\b(a|b|c)\b/);#得到("xxxx", "a", "yyyy", "b", "zzzz", "c", "mmmm")
split (/\b(?:a|b|c)\b/);#得到("xxxx", "yyyy","zzzz", "mmmm")
(3)(?=...)和(?!...)这两兄弟叫做是lookhead assertion,往前探脑袋瞧一瞧但并不吃进来。
m/girl(?=boy)/;#看一下"girl"后面是不是跟着一个boy.
m/girl(?!boy)/;#看一下"girl"后面是不是没跟着一个boy.
那么,有同学说了,想看看前面是不是没有boy护卫,怎么看?
这样吗?
m/(?!boy)girl/#Wrong,这设备只能往后看,看不到girl前面的情况,
别急,有办法搞掂!用我们的$`,他就像是个潜望镜,可以看到前面的情况。
if(/girl/ and $`!~/boy$/) #道路虽然曲折,但是还是把女孩子的护卫情况搞清楚了。:D

20. here document 的用法注意
(1)以行为操作单位,所以结束符要单起一行,而且后面不能跟";"。
(2)标记要紧挨着"<<"
####################################################################################
print <<EOP;#print <<"EOP" 两者一样,下面的段落可以进行内插。但是print <<'EOP'不进行内插
xxxxxxxxxxxxxx
xxxxxxxxxxx
EOP

print << x 10;#以空行为结束符,输出下面句子十次。更直观的写法:print <<"" x 10;
Day Day up!

###########################################################################
(3)ft!还可以进行一些变态操作:调用系统或者运算。
print <<`EOC`;
dir
EOC
print <<ADD
123
ABC
+321;#print 444.

21. 多行匹配/m,采用learning perl 中的例子。

Matching Multiple-line Text
Classic regular expressions were used to match just single lines of text. But since Perl can work with strings of any length, Perl's patterns can match multiple lines of text as easily as single lines. Of course, you have to include an expression that holds more than one line of text. Here's a string that's four lines long:

$_ = "I'm much better\nthan Barney is\nat bowling,\nWilma.\n";
Now, the anchors ^ and $ are normally anchors for the start and end of the whole string (see Section 8.3 in Chapter 8). But the /m regular expression option lets them match at internal newlines as well (think m for multiple lines). This makes them anchors for the start and end of each line, rather than the whole string. So this pattern can match:

print "Found 'wilma' at start of line\n" if /^wilma\b/im;
Similarly, you could do a substitution on each line in a multiline string. Here, we read an entire file into one variable,[9] then add the file's name as a prefix at the start of each line:

[9] Hope it's a small one. The file, that is, not the variable.

open FILE, $filename
or die "Can't open '$filename': $!";
my $lines = join '', <FILE>;
$lines =~ s/^/$filename: /gm;

22. trapping errors with eval
Sometimes, your ordinary, everyday code can cause a fatal error in your program. Each of these typical statements could crash a program:

$barney = $fred / $dino; # divide-by-zero error?

print "match\n" if /^($wilma)/; # illegal regular expression error?

open CAVEMAN, $fred # user-generated error from die?
or die "Can't open file '$fred' for input: $!";
You could go to some trouble to catch some of these, but it's hard to get them all. (How could you check the string $wilma from that example to ensure that it makes a valid regular expression?) Fortunately, Perl provides a simple way to catch fatal errors: wrap the code in an eval block:

eval { $barney = $fred / $dino } ;
Now, even if $dino is zero, that line won't crash the program. The eval is actually an expression (not a control structure, like while or foreach) so that semicolon is required at the end of the block.

When a normally fatal error happens during the execution of an eval block, the block is done running, but the program doesn't crash. So that means that right after an eval finishes, you'll be wanting to know whether it exited normally or whether it caught a fatal error for you. The answer is in the special $@ variable. If the eval caught a fatal error, $@ will hold what would have been the program's dying words, perhaps something like: Illegal division by zero at my_program line 12. If there was no error, $@ will be empty. Of course, that means that $@ is a useful Boolean (true/false) value, true if there was an error, so you'll sometimes see code like this after an eval block:

print "An error occurred: $@" if $@;
The eval block is a true block, so it makes a new scope for lexical (my) variables. This piece of a program shows an eval block hard at work:

foreach my $person (qw/ fred wilma betty barney dino pebbles /) {
eval {
open FILE, "<$person"
or die "Can't open file '$person': $!";

my($total, $count);

while (<FILE>) {
$total += $_;
$count++;
}

my $average = $total/$count;
print "Average for file $person was $average\n";

&do_something($person, $average);
};

if ($@) {
print "An error occurred ($@), continuing\n";
}
}
How many possible fatal errors are being trapped here? If there is an error in opening the file, that error is trapped. Calculating the average may divide by zero, so that error is trapped. Even the call to the mysteriously named &do_something subroutine will be protected against fatal errors, because an eval block traps any otherwise-fatal errors that occur during the time that it's active. (This feature is handy if you have to call a subroutine written by someone else, and you don't know whether they've coded defensively enough to avoid crashing your program.)

If an error occurs during the processing of one of the files, we'll get an error message, but the program will go on to the next file without further complaint.

You can nest eval blocks inside other eval blocks. The inner one traps errors while it runs, keeping them from reaching the outer blocks. (Of course, after the inner eval finishes, if it caught an error, you may wish to re-post the error by using die, thereby letting the outer eval catch it.) An eval block traps any errors that occur during its execution, including errors that happen during subroutine calls (as we saw in the example earlier).

We mentioned earlier that the eval is an expression, which is why the trailing semicolon is needed after the closing curly brace. But since it's an expression, it has a return value. If there's no error, it's like a subroutine: the return value is the last expression evaluated, or it's returned early with an optional return keyword. Here's another way to do the math without having to worry about divide-by-zero:

my $barney = eval { $fred / $dino };
If the eval traps a fatal error, the return value is either undef or an empty list, depending upon the context. So in the previous example, $barney is either the correct result from dividing, or it's undef; we don't really need to check $@ (although it's probably a good idea to check defined($barney) before we use it further).

There are four kinds of problems that eval can't trap. The first group are the very serious errors that crash Perl itself, such as running out of memory or getting an untrapped signal. Since Perl itself isn't running, there's no way it can trap these errors.[2]

[2] Some of these errors are listed with an (X) code on the perldiag manpage, if you're curious.

Of course, syntax errors inside the eval block are caught at compile time—they're never returned in $@.

The exit operator terminates the program at once, even if it's called from a subroutine inside an eval block. (This correctly implies that when writing a subroutine, you should use die rather than exit to signal when something goes wrong.)

The fourth and final kind of problem that an eval block can't trap are warnings, either user-generated ones (from warn) or Perl's internally generated warnings (requested with the -w command-line option or the use warnings pragma). There's a separate mechanism from eval for trapping warnings; see the discussion of the __WARN__ pseudosignal in the Perl documentation for the details.

We should also mention that there's another form of eval that can be dangerous if it's mishandled. In fact, you'll sometimes run across someone who will say that you shouldn't use eval in your code for security reasons. They're (mostly) right that eval should be used only with great care, but they're talking about the other form of eval, sometimes called "eval of a string". If the keyword eval is followed directly by a block of code in curly braces, as we're doing here, there's no need to worry—that's the safe kind of eval.


23. map grep 也采用learning perl 中的例子。
##Picking Items from a List with grep
Sometimes you'll want only certain items from a list. Maybe it's only the odd numbers selected from a list of numbers, or maybe it's only the lines mentioning Fred from a file of text. As we'll see in this section, picking some items from a list can be done simply with the grep operator.

Let's try that first one and get the odd numbers from a large list of numbers. We don't need anything new to do that:

my @odd_numbers;

foreach (1..1000) {
push @odd_numbers, $_ if $_ % 2;
}
That code uses the modulus operator (%), which we saw in Chapter 2. If a number is even, that number "mod two" gives zero, which is false. But an odd number will give one; since that's true, only the odd numbers will be pushed onto the array.

Now, there's nothing wrong with that code as it stands—except that it's a little longer to write and slower to run than it might be, since Perl provides the grep operator:

my @odd_numbers = grep { $_ % 2 } 1..1000;
That line gets a list of 500 odd numbers in one quick line of code. How does it work? The first argument to grep is a block that uses $_ as a placeholder for each item in the list, and returns a Boolean (true/false) value. The remaining arguments are the list of items to search through. The grep operator will evaluate the expression once for each item in the list, much as our original foreach loop did. For the ones where the last expression of the block returns a true value, that element is included in the list that results from grep.

While the grep is running, $_ is aliased to one element of the list after another. We've seen this behavior before, in the foreach loop. It's generally a bad idea to modify $_ inside the grep expression, because this will damage the original data.

The grep operator shares its name with a classic Unix utility that picks matching lines from a file by using regular expressions. We can do that with Perl's grep, which is much more powerful. Here we pull only the lines mentioning fred from a file:

my @matching_lines = grep { /\bfred\b/i } <FILE>;
There's a simpler syntax for grep, too. If all you need for the selector is a simple expression (rather than a whole block), you can just use that expression, followed by a comma, in place of the block. Here's the simpler way to write that latest example:

my @matching_lines = grep /\bfred\b/i, <FILE>;

##Transforming Items from a List with map
Another common task is transforming items from a list. For example, suppose you have a list of numbers that should be formatted as "money numbers" for output, as with the subroutine &big_money (from Chapter 15). But we don't want to modify the original data; we need a modified copy of the list just for output. Here's one way to do that:

my @data = (4.75, 1.5, 2, 1234, 6.9456, 12345678.9, 29.95);
my @formatted_data;

foreach (@data) {
push @formatted_data, &big_money($_);
}
That looks similar in form to the example code used at the beginning of the section on grep, doesn't it? So it may not surprise you that the replacement code resembles the first grep example:

my @data = (4.75, 1.5, 2, 1234, 6.9456, 12345678.9, 29.95);

my @formatted_data = map { &big_money($_) } @data;
The map operator looks much like grep because it has the same kind of arguments: a block that uses $_, and a list of items to process. And it operates in a similar way, evaluating the block once for each item in the list, with $_ aliased to a different original list element each time. But the last expression of the block is used differently; instead of giving a Boolean value, the final value actually becomes part of the resulting list.[3]

[3] One other important difference is that the expression used by map is evaluated in a list context and may return any number of items, not necessarily one each time.

Any grep or map statement could be rewritten as a foreach loop pushing items onto a temporary array. But the shorter way is typically more efficient and more convenient. Since the result of map or grep is a list, it can be passed directly to another function. Here we can print that list of formatted "money numbers" as an indented list under a heading:

print "The money numbers are:\n",
map { sprintf("%25s\n", $_) } @formatted_data;
Of course, we could have done that processing all at once, without even the temporary array @formatted_data:

my @data = (4.75, 1.5, 2, 1234, 6.9456, 12345678.9, 29.95);
print "The money numbers are:\n",
map { sprintf("%25s\n", &big_money($_) ) } @data;
As we saw with grep, there's also a simpler syntax for map. If all you need for the selector is a simple expression (rather than a whole block), you can just use that expression, followed by a comma, in place of the block:

print "Some powers of two are:\n",
map "\t" . ( 2 ** $_ ) . "\n", 0..15;

24. 数组下标,$#array=-1 when @array undef. 数组扩充与缩减。
多位数组的实现以及插入行列,以及遍历时看作reference 来访问。
参见Programming Perl 2ed version
或者Perllol
25. 数字注意事项
prefix = "0x"表示16进制
prefix = "0"表示8进制
大数据用下划线"12_3_4___5",多几个也没事。
26. q{}的妙用
$chunck_of_code = q{
if($condition){
print "hulala";
}
}
27. glob file operator <wantfile> <*> <asa*>
获取目录下文件的简便方法。

分享到:
评论

相关推荐

    perl 学习笔记和心得

    Perl确实是一种非常吸引人的程序语言,而之所以这么引人入胜的原因不单单在于他的功能,也在于他写作的方式,或说成为一种程序写作的艺术。即使你只是每天埋首于程序写作的程序设计师,也不再让生活过份单调,至少你...

    perl脚本语言学习笔记心得

    这个文档中记录和总结了我学习perl脚本语言的一些经验和初学者需要注意的事情

    Programming Perl中文版

    - Reddit 的 r/perl 社区:与其他 Perl 爱好者交流心得。 - **开源项目:** - GitHub 上有大量使用 Perl 编写的开源项目,可以从中学习实际应用案例。 #### 五、总结 “Programming Perl中文版”不仅适合初学者...

    关于基础perl和shell编写的一点点心得

    这篇文章将探讨这两种语言的基础知识,以及它们在实际工作中的应用心得。 首先,Perl( Practical Extraction and Reporting Language)是一种强大的文本处理工具,尤其擅长处理复杂的文本模式匹配和数据提取任务。...

    Perl 語言入門.rar

    这个"Perl语言入门"压缩包文件很可能是作者在学习Perl过程中积累的一些资料和心得,旨在帮助初学者快速入门。以下是对Perl语言的一些核心知识点的详细介绍: 1. **Perl简介**:Perl是由Larry Wall于1987年开发的...

    高级Perl编程(黑豹书)+学习手札

    而《Perl_学习手札》可能是一本个人学习笔记或者实践总结,它可能包含作者在学习Perl过程中的心得、常见问题解决方案和实际项目经验。这样的书籍通常更具实战性,能够帮助读者在解决实际问题时找到灵感。 通过结合...

    Manning - Graphics Programming with Perl

    - 参与开源项目或社区,与其他开发者交流心得,共同进步。 综上所述,《Manning - Graphics Programming with Perl》是一本针对Perl图形编程领域的专业书籍,不仅涵盖了基础理论知识,还提供了丰富的实战案例。无论...

    Eclipse 3.3 中文PERL插件

    2. **perl学习笔记.doc**:个人学习Perl的心得笔记,可能涵盖了基本语法、常见问题和实用技巧。 3. **Shell_编程_教程.ppt**:虽然不是直接关于Perl,但Shell编程与Perl有密切关系,了解Shell可以帮助理解系统级操作...

    perl的学习笔记1

    这份"perl的学习笔记1"很可能是作者在深入学习Perl过程中积累的宝贵资料,包括他自己编写的小程序和学习心得。 首先,Perl语言的核心特性是其灵活性和适应性。它的语法混合了C、sed、awk等语言的特点,使得它既适合...

    programming the network with perl

    - **参与社区**:加入Perl社区或者相关的技术论坛,与其他开发者交流心得,可以获得宝贵的建议和支持。 通过以上内容的学习,读者不仅可以深入了解Perl在网络编程中的强大功能,还能掌握一系列实用的技术和方法,...

    Perl语言入门 第5版 中文

    3. **参与社区**:加入Perl相关的在线论坛或社区,与其他开发者交流经验和心得。 4. **持续跟进**:Perl语言不断发展和完善,定期关注最新版本的功能更新和技术动态。 ### 结语 《Perl语言入门 第5版》是一本非常...

    Perl语言入门(第五版).pdf

    这对于学习者来说非常有帮助,因为它允许用户在阅读的同时添加自己的注解,方便回顾和整理学习心得,也便于在学习过程中进行针对性的搜索,快速找到需要复习的内容。带标签功能则表示文档内部可能含有目录、索引或...

    perl catalyst book

    - **论坛交流**:参与官方论坛或其他技术社区,与其他开发者交流心得,获取技术支持。 - **开源项目**:通过GitHub等平台查找相关开源项目,了解实际应用案例,学习最佳实践。 ### 结语 《The Definitive Guide to...

    linux学习心得 多年的总结

    【Linux学习心得】 Linux操作系统是计算机领域中一个重要的开源系统,由芬兰的Linus Torvalds于1991年10月首次发布0.02版本,直到1994年3月发布了1.0版本,标志着其成熟度的一个重要里程碑。Linux的发展受益于GPL...

    MTK开发心得.pdf

    ### MTK开发心得知识点概述 #### 一、开发环境设立 **1. 开发主机要求及所需工具** - **操作系统**: Windows 2000, WinXP. 推荐使用 Windows 2000 with SP2 or later。 - **编译器**: ADS (Arm Developer Suite) ...

    MTK开发心得 中文

    - **检查 Perl 版本**:在命令行窗口输入 `perl -v`,如果显示版本为 ActivePerl 5.6.1,则表示安装成功。 **注 1**:如果在安装过程中更改了默认路径,需要更新源码中的相应配置。例如,在 `make\Option.mak` 文件...

Global site tag (gtag.js) - Google Analytics