On 18 Jun 2001 14:22:06 -0500, Nick Transier wrote:
> Does the C++ notion of private data have a similar structure in perl, when
> defining packages, I find that when I try to define global variables inside
> the package, but outside of all the subroutines, I get a million errors.
> Thanks,
>
> -Nick
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com
>
>
By global variables I assume you mean class variables (ie variables that
are the same for all objects of that class). You can declare class
variables like this:
<code>
Package Foo;
{ #Foo's class variables
my $num_of_objects = 0;
sub get_num_of_objects { return $num_of_objects }
sub set_num_of_objects { $num_of_objects = $_[0] }
} # $num_of_objects would normally go out of scope here,
# but the two functions act a references, this is called closure
# and it makes my head hurt
sub new {
my $class = ref($_[0]) || $_[0];
$class->set_num_of_objects($class->get_num_of_objects + 1);
return bless { 'bar' => 'baz' };
}
</code>
NOTE: Only get_num_of_objects and set_num_of_objects have access to
$num_of_objects since for everything else it is out of scope. Whoever
thought up closure was a sick, sick individual.
If you are planning on doing OOP in Perl run -- do not walk -- to the
nearest bookstore and buy _Object Oriented Perl_ by Damian Conway.
--
Today is Prickle-Prickle, the 23rd day of Confusion in the YOLD 3167
Keep the Lasagna flying!