I've just begun designing a Perl class, and my only prior experience with OOP is with C++, a long time ago.
There are a few items of data that I need to be "class variables" - shared by all instances. I'd like for them to be initialized prior to the first time I instantiate an object, and I'd like for the main program that issues use MyClass
to be able to provide a parameter for that initialization process.
Here's a working example of a class with a class variable:
package MyClass;
use strict;
use warnings;
# class variable ('our' for package visibility)
#
our $class_variable = 3; # Would like to bind to a variable
sub new {
my $class = shift;
my $self = { };
bless $self, $class;
return $self;
}
sub method {
my $self = shift;
print "class_variable: $class_variable\n";
++$class_variable; # prove that other instances will see this change
}
And here's a demo:
#!/usr/bin/perl
use strict;
use warnings;
use MyClass;
my $foo = MyClass->new();
$foo->method(); # show the class variable, and increment it.
my $bar = MyClass->new();
$bar->method(); # this will show the incremented class variable.
Is there any way for the main program to specify a value for $class_variable? The value would be known at compile time in the main program.