RE: How to put a global variable in a package, accessible to users of that package?
- From: stewart.anderson@xxxxxxxxxxxxx (Stewart Anderson)
- Date: Fri, 31 Oct 2008 11:55:10 -0000
-----Original Message-----package-
From: Rob Dixon [mailto:rob.dixon@xxxxxxx]
Sent: 31 October 2008 11:51
To: Perl Beginners
Cc: Stewart Anderson
Subject: Re: How to put a global variable in a package, accessible to
users of that package?
Stewart Anderson wrote:
From: mrstevegross [mailto:mrstevegross@xxxxxxxxx]
I have a package named "Foo" in which I want to define some
work:level constants (such as $VAR="soemval"). I want those constants
available to users of package Foo, so the following code would
whatThat looks interesting. How do people use that kind of constant
=== 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
beingothers 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
defined.definition
- The Perl compiler has a chance to optimise out the subroutine
andfuture
call
- The implementation could change to something more optimal in the
without needing to alter any code that uses the pragmaThanks :)
I hope this helps,
Rob
Stu
Information in this email including any attachments may be privileged, confidential and is intended exclusively for the addressee. The views expressed may not be official policy, but the personal views of the originator. If you have received it in error, please notify the sender by return e-mail and delete it from your system. You should not reproduce, distribute, store, retransmit, use or disclose its contents to anyone. Please note we reserve the right to monitor all e-mail communication through our internal and external networks. SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and are used under licence. British Sky Broadcasting Limited (Registration No. 2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited (Registration No. 2340150) are direct or indirect subsidiaries of British Sky Broadcasting Group plc (Registration No. 2247735). All of the companies mentioned in this paragraph are incorporated in England and Wales and share the same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.
.
- 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"
- Re: How to put a global variable in a package, accessible to users of that package?
- From: Rob Dixon
- 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: Issue with references and array slice with one member !
- 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
|