7
是最好還是最差的方法之一?我應該如何將對象傳遞給子例程?
利用範圍:
my $cache = CHI->new(driver => 'File', expires_in => 3600);
sub one {
if (my $data = $cache->get('key_one')) {
# ...
}
sub two {
if (my $data = $cache->get('key_two')) {
# ...
}
傳遞對象作爲參數:
my $cache = CHI->new(driver => 'File', expires_in => 3600);
sub one {
my ($cache) = @_;
if (my $data = $cache->get('key_one')) {
# ...
}
sub two {
my ($argument1, $cache) = @_;
if (my $data = $cache->get('key_two')) {
# ...
}
或在子程序創建一個新的實例:
sub one {
my $cache = CHI->new(driver => 'File', expires_in => 3600);
if (my $data = $cache->get('key_one')) {
# ...
}
sub two {
my $cache = CHI->new(driver => 'File', expires_in => 3600);
if (my $data = $cache->get('key_two')) {
# ...
}