Not everything criticized by perlcritic has to be fixed. But usually, it really is preferable to specify the package name for bless
.
If you don't declare a package name, your code implicitly is in package main
. If you don't care about the actual name and want to refer to the current package, use the __PACKAGE__
symbol, e.g.:
my $self = bless {} => __PACKAGE__;
You could also write a little bit of boilerplate:
sub new {
my ($class) = @_;
return bless {} => $class;
}
my $self = __PACKAGE__->new;
Or you could use an object system like Moo
:
use Moo;
has _time_stamp => (is => 'rw');
sub time_stamp {
my ($self, @args) = @_;
if( 1 < scalar @args ) {
$self->_time_stamp(strftime "%Y%m%d_%H%M%S", @args);
}
return $self->_time_stamp;
}
my $self = __PACKAGE__->new; # a "new" is autogenerated