我习惯于使用$self
for OO Perl,即使我只是编写独立的脚本而根本没有命名我的包。例如,我script.pl
会这样开始:
use strict;
use warnings;
my $self = bless {};
$self->time_stamp(localtime(time()));
1;
sub time_stamp {
my ($self,@args) = @_;
# Getter/setter
my $ftime_stamp;
if( 1 < scalar @args ) {
$ftime_stamp = strftime "%Y%m%d_%H%M%S", @args;
$self->{'time_stamp'} = $ftime_stamp;
}
return $self->{'time_stamp'};
}
[...]
这些脚本有时会变成适当的模块,但有时我只是让它们如此。
这虽然违背了 Perl Critic of "One-argument blesses":
One-argument "bless" used at line 162, column 12. See page 365 of PBP. Severity: 5
有没有一种简洁的方式来命名我的祝福,但仍然包含我的所有内容script.pl
?