没有合适的资源?快使用搜索试试~ 我知道了~
首页PERL6入门教程:独特性能探索
PERL6入门教程:独特性能探索
需积分: 9 5 下载量 10 浏览量
更新于2024-07-21
1
收藏 465KB PDF 举报
PERL6学习教程是一本详细介绍Perl 6语言的教材,Perl 6是Perl 5家族的新成员,它在语法、性能和设计理念上有着显著的改进。本书的目标读者是对Perl语言有一定基础,或者希望学习现代化动态语言的人群。 该书的结构清晰,首先在前言部分,作者们——包括Jonathan S. Duff, Moritz Lenz, Carl Mäsak, Patrick R. Michaud, Jonathan Worthington等,对目标受众、书籍格式以及Perl 6与Perl 5之间的关系进行了介绍。Perl 6的设计旨在提供更简洁、一致和直观的编程体验,同时保持了Perl的传统灵活性。 前言还提到了Perl 6的实现情况,主要关注Rakudo Perl 6实现,这是Perl 6的一个主要实现者。书中指导读者如何安装Rakudo,这对于初学者来说是非常实用的步骤。执行Perl 6程序也是章节内容的一部分,让读者了解如何在实际环境中运行新语言的代码。 此外,作者鼓励读者参与到Perl 6的社区中,分享知识,参与讨论和贡献,以促进语言的发展。在接下来的章节"2. The Basics",读者将学习Perl 6的基础概念,如变量、数据类型、控制结构、函数和模块等,并通过练习题(exercises)加深理解,确保理论与实践相结合。 这本书遵循Creative Commons BY-NC-SA 3.0许可协议,这意味着读者可以在非商业用途下自由复制、修改和分享内容,同时也尊重了作者的署名权。这是一本适合想要深入了解Perl 6语言并提升编程技能的读者的实用指南。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/9372603/bg10.jpg)
Chapter 2 THE BASICS
Before these two lines execute, %sets is empty. Adding to an entry that is not in the hash
yet will cause that entry to spring into existence just-in-time, with a value starting at zero.
(is is autovivication). Aer these two lines have run for the rst time, %sets contains
'Ana' => 3, 'Dave' => 0. (e fat arrow => separates key and value in a Pair.)
1 if $r1 > $r2 {
2 %matches{$p1}++;
3 } else {
4 %matches{$p2}++;
5 }
If $r1 has a larger value than $r2, %matches{$p1} increments by one. If $r1 is not larger
than $r2, %matches{$p2} increments. Just as in the case of +=, if either hash value did not
exist previously, it is autovivied by the increment operation.
$thing++ is short for $thing += 1 or $thing = $thing + 1, with the small exception that
the return value of the expression is $thing before the increment, not the incremented
value. As in many other programming languages, you can use ++ as a prex. en it
returns the incremented value; my $x = 1; say ++$x prints 2.
1 my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
is line consists of three individually simple steps. An array’s sort method returns a
sorted version of the array’s contents. However, the default sort on an array sorts by its
contents. To print player names in winner-rst order, the code must sort the array by
the scores of the players, not their names. e sort method’s argument is a block used
to transform the array elements (the names of players) to the data by which to sort. e
array items are passed in through the topic variable $ .
You have seen blocks before: both the for loop -> $line { ... } and the if state-
ment worked on blocks. A block is a self-contained piece of Perl 6 code with an optional
signature (the -> $line part). See sec:signatures on page 30 for more information.
e simplest way to sort the players by score would be @names.sort({ %matches{$ } }),
which sorts by number of matches won. However Ana and Dave have both won two
matches. at simple sort doesn’t account for the number of sets won, which is the sec-
ondary criterion to decide who has won the tournament.
When two array items have the same value, sort leaves them in the same order as it
found them. Computer scientists call this a stable sort. e program takes advantage of
10
![](https://csdnimg.cn/release/download_crawler_static/9372603/bg11.jpg)
this property of Perl 6’s sort to achieve the goal by sorting twice: rst by the number of
sets won (the secondary criterion), then by the number of matches won.
Aer the rst sorting step, the names are in the order Beth Charlie Dave Ana. Aer
the second sorting step, it’s still the same, because no one has won fewer matches but
more sets than someone else. Such a situation is entirely possible, especially at larger
tournaments.
sort sorts in ascending order, from smallest to largest. is is the opposite of the desired
order. erefore, the code calls the .reverse method on the result of the second sort, and
stores the nal list in @sorted.
1 for @sorted -> $n {
2 say "$n has won %matches{$n} matches and %sets{$n} sets";
3 }
To print out the players and their scores, the code loops over @sorted, setting $n to the
name of each player in turn. Read this code as “For each element of sorted, set $n to
the element, then execute the contents of the following block.” say prints its arguments
to the standard output (the screen, normally), followed by a newline. (Use print if you
don’t want the newline at the end.)
When you run the program, you’ll see that say doesn’t print the contents of that string
verbatim. In place of $n it prints the contents of the variable $n– the names of players
stored in $n. is automatic substitution of code with its contents is interpolation. is
interpolation happens only in strings delimited by double quotes "...". Single quoted
strings '...' do not interpolate:
1 my $names = 'things';
2 say 'Do not call me $names'; # Do not call me $names
3 say "Do not call me $names"; # Do not call me things
Double quoted strings in Perl 6 can interpolate variables with the $ sigil as well as blocks
of code in curly braces. Since any arbitrary Perl code can appear within curly braces,
Arrays and Hashes may be interpolated by placing them within curly braces.
Arrays within curly braces are interpolated with a single space character between each
item. Hashes within curly braces are interpolated as a series of lines. Each line will con-
tain a key, followed by a tab character, then the value associated with that key, and nally
a newline.
11
![](https://csdnimg.cn/release/download_crawler_static/9372603/bg12.jpg)
Chapter 2 THE BASICS
TODO: explain <...> quote-words
1 say "Math: { 1 + 2 }" # Math: 3
2 my @people = <Luke Matthew Mark>;
3 say "The synoptics are: {@people}" # The synoptics are: Luke Matthew Mark
4
5 say "{%sets}"; # From the table tennis tournament
6
7 # Charlie 4
8 # Dave 6
9 # Ana 8
10 # Beth 4
When array and hash variables appear directly in a double-quoted string (and not inside
curly brackets), they are only interpolated if their name is followed by a postcircumx –
a bracketing pair that follows a statement. It’s also ok to have a method call between the
variable name and the postcircumx.
1 my @flavours = <vanilla peach>;
2
3 say "we have @flavours"; # we have @flavours
4 say "we have @flavours[0]"; # we have vanilla
5 # so-called "Zen slice"
6 say "we have @flavours[]"; # we have vanilla peach
7
8 # method calls ending in postcircumfix
9 say "we have @flavours.sort()"; # we have peach vanilla
10
11 # chained method calls:
12 say "we have @flavours.sort.join(', ')";
13 # we have peach, vanilla
2.1 Exercises
1. e input format of the example program is redundant: the rst line containing the
name of all players is not necessary, because you can nd out which players participated
in the tournament by looking at their names in the subsequent rows.
12
![](https://csdnimg.cn/release/download_crawler_static/9372603/bg13.jpg)
How can you change the program if the rst input line is omitted? Hint: %hash.keys
returns a list of all keys stored in %hash.
Answer: Remove the line my @names = $file.get.words;, and change:
1 my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
... into:
1 my @sorted = %sets.keys.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
2. Instead of removing the redundancy, you can also use it to warn if a player appears
that wasn’t mentioned in the rst line, for example due to a typo. How would you modify
your program to achieve that?
Answer: Introduce another hash with the names of the legitimate players as keys, and
look in this hash when the name of a player is read:
1 ...
2 my @names = $file.get.split(' ');
3 my %legitimate-players;
4 for @names -> $n {
5 %legitimate-players{$n} = 1;
6 }
7
8 ...
9
10 for $file.lines -> $line {
11 my ($pairing, $result) = $line.split(' | ');
12 my ($p1, $p2) = $pairing.split(' ');
13 for $p1, $p2 -> $p {
14 if !%legitimate-players{$p} {
15 say "Warning: '$p' is not on our list!";
16 }
17 }
18
19 ...
20 }
13
![](https://csdnimg.cn/release/download_crawler_static/9372603/bg14.jpg)
剩余134页未读,继续阅读
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/e7dd7c359c4741369ee3a7fedab2a5ad_qium2014.jpg!1)
海云之家
- 粉丝: 72
- 资源: 3
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- 计算机人脸表情动画技术发展综述
- 关系数据库的关键字搜索技术综述:模型、架构与未来趋势
- 迭代自适应逆滤波在语音情感识别中的应用
- 概念知识树在旅游领域智能分析中的应用
- 构建is-a层次与OWL本体集成:理论与算法
- 基于语义元的相似度计算方法研究:改进与有效性验证
- 网格梯度多密度聚类算法:去噪与高效聚类
- 网格服务工作流动态调度算法PGSWA研究
- 突发事件连锁反应网络模型与应急预警分析
- BA网络上的病毒营销与网站推广仿真研究
- 离散HSMM故障预测模型:有效提升系统状态预测
- 煤矿安全评价:信息融合与可拓理论的应用
- 多维度Petri网工作流模型MD_WFN:统一建模与应用研究
- 面向过程追踪的知识安全描述方法
- 基于收益的软件过程资源调度优化策略
- 多核环境下基于数据流Java的Web服务器优化实现提升性能
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)