Re: How to put a global variable in a package, accessible to users of that package?
- From: rob.dixon@xxxxxxx (Rob Dixon)
- Date: Fri, 31 Oct 2008 11:51:17 +0000
Stewart Anderson wrote:
From: mrstevegross [mailto:mrstevegross@xxxxxxxxx]That looks interesting. How do people use that kind of constant
I have a package named "Foo" in which I want to define some package-
level constants (such as $VAR="soemval"). I want those constants
available to users of package Foo, so the following code would work:
=== foo.pl ===
package foo;
use constant VAR => "someval";
=== bar.pl ===
use foo;
print $foo::VAR;
It doesn't appear to be working; it compiles ok, but it prints
nothing. I thought it would print "someval".
assignment. I can see uses for it, but would be interested to hear what
others use this technique for.
Take a look at
perldoc constant
What the pragma creates is a prototyped subroutine with exactly zero parameters,
so writing
use constant PI => 3.14159265359;
is equivalent to a subroutine
sub PI() {
3.14159265359;
}
However it has a few of advantages over just writing this directly:
- It is self-documenting, i.e. it is clear that a named constant is being
defined.
- The Perl compiler has a chance to optimise out the subroutine definition and
call
- The implementation could change to something more optimal in the future
without needing to alter any code that uses the pragma
I hope this helps,
Rob
.
- Follow-Ups:
- RE: How to put a global variable in a package, accessible to users of that package?
- From: Stewart Anderson
- RE: How to put a global variable in a package, accessible to users of that package?
- References:
- How to put a global variable in a package, accessible to users of that package?
- From: Mrstevegross
- RE: How to put a global variable in a package, accessible to users of that package?
- From: "Stewart Anderson"
- How to put a global variable in a package, accessible to users of that package?
- Prev by Date: RE: How to put a global variable in a package, accessible to users of that package?
- Next by Date: RE: How to put a global variable in a package, accessible to users of that package?
- Previous by thread: RE: How to put a global variable in a package, accessible to users of that package?
- Next by thread: RE: How to put a global variable in a package, accessible to users of that package?
- Index(es):
Relevant Pages
|