没有合适的资源?快使用搜索试试~ 我知道了~
首页现代Perl第4版:从新手到高手的全面指南
"《现代Perl第4版》是一本深入讲解Perl语言的权威指南,由经验丰富的专家Chromatic撰写,全面阐述了Perl从哲学基础到实用技巧,适合从新手到高级程序员的学习者。本书特别关注了Perl 5.20和5.22的新特性和改进,包括新的运算符、标准库变化、性能提升和安全修复。 在阅读这本书时,读者会了解到Perl语言的核心理念,如何利用社区积累的丰富资源,以及编写高效、可维护代码的方法。书中详细介绍了各种编程概念,如操作符、函数、正则表达式、对象导向编程、编码风格和效能优化等。此外,作者还指导如何使用Moose框架、编写可测试的代码、部署和维护实际应用。 对于那些初次接触Perl或想要升级技能的人来说,本书是不可或缺的工具。它不仅教授基本语法,还深入探讨了哪些特性有助于提高生产力,以及哪些应避免过时的技术。书中通过丰富的例子和实战,帮助读者逐步成长为精通Perl的黑客,无论是处理简单的命令行脚本还是构建复杂的Unicode支持的Web服务。 无论你是希望解决当前问题,还是寻求一种强大的问题解决语言,现代Perl都能满足你的需求。随着Perl 5.22的更新,本书确保你能够充分利用最新版本的Perl,提升日常编程效率。对于教学和实际工作场景,这本书都具有很高的价值,是Perl开发者和学习者的必读之作。"
资源详情
资源推荐
good taste with experience. Express yourself, but be mindful of readability
and maintainability, especially for those who come after you.
Perl novices often find certain syntactic constructs opaque. These idioms (
Idioms on page 213) offer great (if subtle) power to experienced programmers,
but it’s okay to avoid them until you’re comfortable with them.
As another design goal, Perl tries to avoid surprising experienced (Perl) pro-
grammers. For example, adding two variables (
$first_num + $second_num
) is obvi-
ously a numeric operation (Numeric Operators on page 87). You’ve expressed
your intent to treat the values of those variables as numbers by using a
numeric operator. Perl happily does so. No matter the contents of
$first_num
and
$second_num
, Perl will coerce them to numeric values (Numeric Coercion
on page 67).
Perl adepts often call this principle DWIM, or do what I mean. You could just
as well call this the principle of least astonishment. Given a cursory under-
standing of Perl (especially context; Context on page 5), it should be possible
to understand the intent of an unfamiliar Perl expression. You will develop
this skill as you learn Perl.
Perl’s expressivity allows novices to write useful programs without having to
understand the entire language. This is by design! Experienced developers
often call the results baby Perl as a term of endearment. Everyone begins as
a novice. Through practice and learning from more experienced programmers,
you’ll understand and adopt more powerful idioms and techniques. It’s okay
for you to write simple code that you understand. Keep practicing and you’ll
become a native speaker.
A novice Perl hacker might triple a list of numbers with this:
my @tripled;
for (my $i = 0; $i < scalar @numbers; $i++) {
$tripled[$i] = $numbers[$i] * 3;
}
And a Perl adept might write the following:
my @tripled;
for my $num (@numbers) {
push @tripled, $num * 3;
}
While an experienced Perl hacker could write this:
my @tripled = map { $_ * 3 } @numbers;
Chapter 1. The Perl Philosophy • 4
report erratum • discuss
Every one of these three programs generates the same result. Each uses Perl
in a different way.
As you get more comfortable with Perl, you can let the language do more for
you. With experience, you can focus on what you want to do rather than how
to do it. Perl doesn’t care if you write baby or expert code. Design and refine
your programs for clarity, expressivity, reuse, and maintainability, in part or
in whole. Take advantage of this flexibility and pragmatism: it’s far better to
accomplish your task effectively now than to write a conceptually pure and
beautiful program next year.
Context
In spoken languages, the meaning of a word or phrase depends on how you
use it; the local context of other grammatical constructs helps clarify the
intent. For example, the inappropriate pluralization of “Please give me one
hamburgers!” sounds wrong (the pluralization of the noun differs from the
amount), just as the incorrect gender of “la gato” (the article is feminine, but
the noun is masculine) makes native speakers chuckle. Some words do double
duty; one sheep is a sheep just as two sheep are also sheep and you program
a program.
Perl uses context to express how to treat a piece of data. This governs the
amount of data as well as the kind of data. For example, several Perl operations
produce different behaviors when you expect zero, one, or many results. A
specific construct in Perl may do something different if you write “Do this,
but I don’t care about any results” compared to “Do this and give me multiple
results.” Other operations allow you to specify whether you expect to work
with numeric, textual, or true or false data.
You must keep context in mind when you read Perl code. Every expression
is part of a larger context. You may find yourself slapping your forehead after
a long debugging session when you discover that your assumptions about
context were incorrect. If instead you’re aware of context, your code will be
more correct—and cleaner, flexible, and more concise.
Void, Scalar, and List Context
Amount context governs how many items you expect an operation to produce.
Think of subject-verb number agreement in English. Even without knowing
the formal description of this principle, you probably understand the error
in the sentence “Perl are a fun language.” (In terms of amount context, you
report erratum • discuss
Context • 5
could say that the verb are expects a plural noun or noun phrase.) In Perl,
the number of items you request influences how many you receive.
Suppose the function (Declaring Functions on page 91) called
find_chores()
sorts
your household todo list in order of priority. The number of chores you expect
to read from your list influences what the function produces. If you expect
nothing, you’re just pretending to be busy. If you expect one task, you have
something to do for the next fifteen minutes. If you have a burst of energy on
a free weekend, you could get all of your chores.
Why does context matter? A context-aware function can examine its calling
context and decide how much work it must do. When you call a function and
never use its return value, you’ve used void context:
find_chores();
Assigning the function’s return value to a single item (Scalars on page 50)
enforces scalar context:
my $single_result = find_chores();
Assigning the results of calling the function to an array (Arrays on page 52)
or a list, or using it in a list, evaluates the function in list context:
my @all_results = find_chores();
my ($single_element, @rest) = find_chores();
# list of results passed to a function
process_list_of_results( find_chores() );
The parentheses in the second line of the previous example group the two
variable declarations (Lexical Scope on page 104) into a single unit so that
assignment assigns to both of the variables. A single-item list is still a list,
though. You could also correctly write this:
my ($single_element) = find_chores();
In this case the parentheses tell the Perl parser that you intend list context
for the single variable
$single_element
. This is subtle, but now that you know
about it, the difference of amount context between these two statements
should be obvious:
my $scalar_context = find_chores();
my ($list_context) = find_chores();
Lists propagate list context to the expressions they contain. This often con-
fuses novices until they understand it. Both of these calls to
find_chores()
occur
in list context:
Chapter 1. The Perl Philosophy • 6
report erratum • discuss
process_list_of_results( find_chores() );
my %results = (
cheap_operation => $cheap_results,
expensive_operation => find_chores(), # OOPS!
);
Yes, initializing a hash (Hashes on page 58) with a list of values imposes list
context on
find_chores
. Use the
scalar
operator to impose scalar context:
my %results = (
cheap_operation => $cheap_results,
expensive_operation => scalar find_chores(),
);
Again, context can help you determine how much work a function should do.
In void context,
find_chores()
may legitimately do nothing. In scalar context, it
can find only the most important task. In list context, it must sort and return
the entire list.
Numeric, String, and Boolean Context
Perl’s other context—value context—influences how Perl interprets a piece of
data. Perl can figure out if you have a number or a string and convert data
between the two types. In exchange for not having to declare explicitly what
type of data a variable contains or a function produces, Perl’s value contexts
provide hints about how to treat that data.
Perl will coerce values to specific proper types (Coercion on page 66) depending
on the operators you use. For example, the
eq
operator tests that two values
contain equivalent string values:
say "Catastrophic crypto fail!" if $alice eq $bob;
You may have had a baffling experience where you know that the strings are
different, but they still compare the same:
my $alice = 'alice';
say "Catastrophic crypto fail!" if $alice == 'Bob';
The
eq
operator treats its operands as strings by enforcing string context on
them, but the
==
operator imposes numeric context. In numeric context, both
strings evaluate to
0
(Numeric Coercion on page 67). Be sure to use the
proper operator for your desired value context.
Boolean context occurs when you use a value in a conditional statement. In
the previous examples,
if
evaluated the results of the
eq
and
==
operators in
boolean context.
report erratum • discuss
Context • 7
In rare circumstances, you may not be able to use the appropriate operator
to enforce value context. To force a numeric context, add zero to a variable.
To force a string context, concatenate a variable with the empty string. To
force a boolean context, double up the negation operator:
my $numeric_x = 0 + $x; # forces numeric context
my $stringy_x = '' . $x; # forces string context
my $boolean_x = !!$x; # forces boolean context
Value contexts are easier to identify than amount contexts. Once you know
which operators provide which contexts (Operator Types on page 87), you’ll
rarely make mistakes.
Implicit Ideas
Perl code can seem dense at first, but it’s full of linguistic shortcuts. These
allow experienced programmers to glance at code and understand its important
implications. Context is one shortcut. Another is default variables—the pro-
gramming equivalent of pronouns.
The Default Scalar Variable
The default scalar variable (or topic variable),
$_
, is most notable in its absence:
many of Perl’s built-in operations work on the contents of
$_
in the absence
of an explicit variable. You can still type
$_
if it makes your code clearer to
you, but it’s often unnecessary.
Many of Perl’s scalar operators (including
chr
,
ord
,
lc
,
length
,
reverse
, and
uc
) work
on the default scalar variable if you don’t provide an alternative. For example,
the
chomp
built-in removes any trailing newline sequence (technically the
contents of
$/
; see
perldoc -f chomp
) from its operand:
my $uncle = "Bob\n";
chomp $uncle;
say "'$uncle'";
$_
behaves the same way in Perl as the pronoun it does in English. Without
an explicit variable,
chomp
removes the trailing newline sequence from
$_
.
When you write
chomp;
, Perl will always chomp it. These two lines of code are
equivalent:
chomp $_;
chomp;
say
and
print
also operate on
$_
in the absence of other arguments:
print; # prints $_ to the current filehandle
say; # prints $_ and a newline to the current filehandle
Chapter 1. The Perl Philosophy • 8
report erratum • discuss
剩余277页未读,继续阅读
ramissue
- 粉丝: 354
- 资源: 1487
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计算机人脸表情动画技术发展综述
- 关系数据库的关键字搜索技术综述:模型、架构与未来趋势
- 迭代自适应逆滤波在语音情感识别中的应用
- 概念知识树在旅游领域智能分析中的应用
- 构建is-a层次与OWL本体集成:理论与算法
- 基于语义元的相似度计算方法研究:改进与有效性验证
- 网格梯度多密度聚类算法:去噪与高效聚类
- 网格服务工作流动态调度算法PGSWA研究
- 突发事件连锁反应网络模型与应急预警分析
- BA网络上的病毒营销与网站推广仿真研究
- 离散HSMM故障预测模型:有效提升系统状态预测
- 煤矿安全评价:信息融合与可拓理论的应用
- 多维度Petri网工作流模型MD_WFN:统一建模与应用研究
- 面向过程追踪的知识安全描述方法
- 基于收益的软件过程资源调度优化策略
- 多核环境下基于数据流Java的Web服务器优化实现提升性能
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功