Используйте ключевое слово our
, которое будет иметь область действия пакета:
package Myclass;
use strict;
use warnings;
our $dt;
sub new {
my $class = shift;
my $self = {};
bless $self, $class
}
sub dt { # Implement getter/setter
shift; # Pulls object off @_ (ignores calling class)
$dt = shift if @_; # If argument provided, sets 'dt' to it
return $dt; # In both cases (getter/setter), returns current $dt
}
1;
А потом:
use strict;
use warnings;
use feature 'say';
use Myclass;
my $obj1 = Myclass->new;
my $obj2 = Myclass->new;
$obj1->dt( 2 ); # Set to 2 via $obj1
say $obj2->dt; # '2'
$obj2->dt( 5 ); # Set to 5 via $obj2
say $obj1->dt; # '5'