my %title_of;
my %ISBN_for;
my @sales_from;
这种写法会使后面的代码可读性极好。
while (my $month = prompt -menu => $MONTH_NAMES) {
for my $book (@catalog) {
print "$ISBN_for{$book} $title_of{$book}: $sales_from[$month]\n";
}
}
对于子程序和方法而言,构成名称的文法规则如下
sub get_record
sub get_record_for
sub eat_cookie
sub eat_previous_cookie
sub build_profile
sub build_execution_profile
sub build_execution_profile_using
如此一来,后面的代码几乎不用加注释。
@config_options = get_record_for($next_client);
for my $option (@config_options) {
build_execution_profile_using($next_client, $option);
} 布尔值
一般以所测试的属性或断言作为命名依据,这也会使条件表达式读起来很自然,通常来讲,这些名称大多以is_或has_开头,当然也有例外,比如。
sub is_valid;
sub metadata_available_for;
sub has_end_tag;
my $loading_finished;
my $has_found_bad_record;
看看使用这种规则的代码可读性吧,如下,几乎是自注释的。
if (is_valid($next_record) && !$loading_finished) {
METADATA:
while (metadata_available_for($next_record)) {
pushu @metadata, get_metadata_for($next_record);
last METADATA if has_end_tag($next_record);
}
}
else {
$has_found_bad_record = 1;
} 引用变量
在引用变量的后面加上_ref,可以防止把引用变量当作普通变量使用。