http://www.perlmonks.org?node_id=812991


in reply to Accessing main::Constant

First things first, accessing a class component in a fashion similar to "print pq::ONE" doesn't quite express OOP as it should, the idea of classes in OOP is to provide for encapsulation of data and methods into objects and that these objects interface the class data and methods they are instances of.

In your code, "my $obj = new pq;" brings an object into existence but to no avail since you've not used it... You declared a constant in the main package, since you wanted to access that constant with a class package and since that class has an object, use the object instead to call/invoke the class method through which you wanted to access that constant in main,

use strict; use warnings; use pq; use constant { FO1 => "value" }; my $obj = new pq; #accessing a class through an object... print $obj->ONE; $obj->pr;
OOP is about hiding data and routines away from the rest of the program to simplify the code and enhnace privacy..


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.