Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need help with constants and package names.

Reply
Thread Tools

Need help with constants and package names.

 
 
Brian McCauley
Guest
Posts: n/a
 
      12-13-2004


Sherm Pendley wrote:

> Terry wrote:
>
>> Something I didn't find in the perldoc was how to export a hash using
>> the 'use constant' pragma. The examples seemed to be geared toward
>> single imports as in your example. The reason I'm using 'use export'
>> with a hash ref is to import multiple constants (I'll have at least
>> fifty by the time I'm done). ie:
>>
>> use constant {
>> one => 1 ,
>> two => 2 ,
>> # ...
>> };
>>
>> How then to pass this list to @EXPORT ? I tried assigning the hash ref
>> to a scalar first and assigning its keys into @ISA


Surely you mean @EXPORT?

>> , but you probably already know that didn't work


It works just fine if you pay proper attension to run-time v.
compile-time issues.

> I haven't found a way to do that either - and believe me I've tried.


BEGIN {
my %constants = (
one => 1 ,
two => 2 ,
# ...
);
require constants;
import constants %constants;
push @EXPORT => keys %constants;
}

 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      12-14-2004
Terry <(E-Mail Removed)> writes:
> Hello all, I'm having trouble with packages and constants. What I'd
> like to have is a single .pm for all constants used by the application.


Others have commented on how, and why (and why not), so I'll just add
my two cents on the wisdom of doing so:

On clpm, we tell everybody to 'use warnings;' and 'use strict;'. Why?
Because, among other things, they force you to declare your variables,
so you can catch when you typo writing one and also so you can trace
the life of your variable more easily. When you import a variable
(or a constant), you're effectively circumventing at least one of
those benefits.

Admittedly, it's worse with variables, where you're potentially
subject to action-at-a-distance when some random code out there
somewhere modifies a variable you didn't know it was modifying, but
even with constants, unless you explicitly import them all, it's very
easy to forget where it's defined and why, if you're in the habit of
exporting such things by default. (And for my next trick, a run-on
sentence the length of War and Peace!)

Just a thought.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      12-17-2004

Quoth Brian McCauley <(E-Mail Removed)>:
> Sherm Pendley wrote:
> > Terry wrote:
> >
> >> Something I didn't find in the perldoc was how to export a hash using
> >> the 'use constant' pragma.

<snip>
>
> It works just fine if you pay proper attension to run-time v.
> compile-time issues.
>
> > I haven't found a way to do that either - and believe me I've tried.

>
> BEGIN {
> my %constants = (
> one => 1 ,
> two => 2 ,
> # ...
> );
> require constants;
> import constants %constants;
> push @EXPORT => keys %constants;
> }


That BEGIN is unnecessary if there is no other code in the file: the
require is happening at BEGIN time anyway, and before import is called.

If there *is* other code in the file, it won't have access to the
constants.

Ben

--
don't get my sympathy hanging out the 15th floor. you've changed the locks 3
times, he still comes reeling though the door, and soon he'll get to you, teach
you how to get to purest hell. you do it to yourself and that's what really
hurts is you do it to yourself just you, you and noone else ** http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      12-19-2004


Ben Morrow wrote:
> Quoth Brian McCauley <(E-Mail Removed)>:
>
>>Sherm Pendley wrote:
>>
>>>Terry wrote:
>>>
>>>
>>>>Something I didn't find in the perldoc was how to export a hash using
>>>>the 'use constant' pragma.

>
> <snip>
>
>>It works just fine if you pay proper attension to run-time v.
>>compile-time issues.
>>
>>
>>>I haven't found a way to do that either - and believe me I've tried.

>>
>>BEGIN {
>> my %constants = (
>> one => 1 ,
>> two => 2 ,
>> # ...
>> );
>> require constants;
>> import constants %constants;
>> push @EXPORT => keys %constants;
>>}

>
>
> That BEGIN is unnecessary if there is no other code in the file: the
> require is happening at BEGIN time anyway, and before import is called.
>
> If there *is* other code in the file, it won't have access to the
> constants.


How do you figure that?

There were several mistakes in my code but that wasn't one of them. The
two lines...

require constants;
import constants %constants;

....should have read...

require constant;
import constant $_ => $constants{$_} for keys %constants;

....and then the code beyond the BEGIN block _can_ access the the constants.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constants across package boundaries bernie@fantasyfarm.com Perl Misc 15 05-14-2008 08:20 PM
Retrieve package constants Agoston Bejo ASP General 0 09-01-2004 09:45 AM
Importing a package and looping through modules in the package Dave Python 2 02-10-2004 08:14 PM
need help with runtime constants El Durango Java 3 02-09-2004 08:50 AM
Accessing constants from another package/file. ed Perl Misc 3 06-26-2003 01:24 AM



Advertisments