Journal(2005) | Blog(2006) | RandomLink | WhoAmI | LiveBookmark | HomePage

<<Previous: 一种代码风格  >>Next: Scalar::Util, List::Util, List::MoreUtils

Tips from Perl Best Practices

Category: BookNotes   Keywords: tips

原因有二:一是 use constant 不能内插。例如:

# 用 Readonly 的两个例子
use Readonly;
Readonly my $DEAR      => 'Greetings to you,';
Readonly my $SINCERELY => 'May Heaven guard you from all misfortune,';

$msg = <<"END_MSG";
$DEAR $target_name

$scam_pitch

$SINCERELY

$fake_name
END_MSG

Readonly my $LINES_PER_PAGE => 42;
$margin{$LINES_PER_PAGE}                 # sets $margin{'42'}
    = $MAX_LINES - $LINES_PER_PAGE;
# 对应麻烦的用 constant
use constant (
    DEAR      => 'Greetings to you,',
    SINCERELY => 'May Heaven guard you from all misfortune,',
);

$msg = DEAR . $target_name
       . "$scam_pitch\n\n"
       . SINCERELY
       . "\n\n$fake_name";

use constant (
    LINES_PER_PAGE => 42
);

$margin{LINES_PER_PAGE}               # sets $margin{'LINES_PER_PAGE'}
    = MAX_LINES - LINES_PER_PAGE;

第二个原因是作用域范围。Readonly 能定义不同的作用域,而 constant 只有包作用域。

use YAML;

# Do
local $YAML::Indent = 4;

# NOT
$YAML::Indent = 4;

注意要初始化此变量。当 local 此变量时,它的值默认为 undef.

local $YAML::Indent; # 这里的值为 undef
local $YAML::Indent = $YAML::Indent; # 这样才可能是你想要的
# Do
for my $client (@clients) {
    $client->tally_hours(  );
    $client->bill_hours(  );
    $client->reset_hours(  );
}

# Do NOT
for my $n (0..$#clients) {
    $clients[$n]->tally_hours(  );
    $clients[$n]->bill_hours(  );
    $clients[$n]->reset_hours(  );
}
# 下两句是一样效果的
my $visible_email_address = reverse $actual_email_address;
my $visible_email_address = scalar reverse $actual_email_address;

# 但这两句效果不一样,子程序默认接受数组。所以可能就没有达到你要的目的。
add_email_addr(reverse $email_address); # 列表环境
add_email_addr(scalar reverse $email_address); # 标量环境
# 对于如下代码
Use Digest::SHA qw( sha512 );

# Sort by SHA-512 digest of scripts
@sorted_scripts
    = sort { sha512($a) cmp sha512($b) } @scripts;

# 请用下面两则代替
my %sha512_of;

# Sort by SHA-512 digest of scripts
@sorted_scripts
    = sort { ($sha512_of{$a} ||= sha512($a))
                     cmp
             ($sha512_of{$b} ||= sha512($b))
           }
           @scripts;

# 或者更简洁
# Make the SHA-512 digest function self-caching...
use Memoize;
memoize('sha512');

# Sort by auto-cached SHA-512 digest of scripts...
@sorted_scripts = sort { sha512($a) cmp sha512($b) } @scripts;

Damain 还推荐了 Sort::Maker 来执行相关 sort 任务。

Finally

说实话我对英文也不是很感冒,所以读起来也是跳着读的。没怎么仔细看。所以也就这样了。

<<Previous: 一种代码风格  >>Next: Scalar::Util, List::Util, List::MoreUtils

Options: +Del.icio.us

Related items Created on 2005-09-13 16:57:52, Last modified on 2005-09-13 21:10:45
Copyright 2004-2005 All Rights Reserved. Powered by Eplanet && Catalyst 5.62.