Click here to get back home

Constants across package boundaries

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Constants across package boundaries bernie 05-13-2008
Posted by bernie on May 13, 2008, 8:36 am
Please log in for more thread options
I've been chasing a compile problem and I just saw the light on what
my trouble is: I use a bunch of "use constant this => ...; use
constant that =>...;" to set parameters for the program. BUT: the
program uses a 'package' or two [it has a small embedded objects in
it]. The trouble is that the package can't access the 'constant' --
that section of the program gets [not surprisingly] bareword errors.
I can fix them by using "main::..." but that's really ugly. Is there
any really clean way to set up truly "global" constants? Thanks!

/Bernie\

Posted by Joost Diepenmaat on May 13, 2008, 9:02 am
Please log in for more thread options
bernie@fantasyfarm.com writes:

> I've been chasing a compile problem and I just saw the light on what
> my trouble is: I use a bunch of "use constant this => ...; use
> constant that =>...;" to set parameters for the program. BUT: the
> program uses a 'package' or two [it has a small embedded objects in
> it]. The trouble is that the package can't access the 'constant' --
> that section of the program gets [not surprisingly] bareword errors.
> I can fix them by using "main::..." but that's really ugly. Is there
> any really clean way to set up truly "global" constants? Thanks!

I think you'd rather just export the constants to any package that
needs them, instead of making them truly global (which would be more
or less equivalent to exporting them to *all* packages)

See perldoc Exporter.



--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by bernie on May 13, 2008, 9:50 am
Please log in for more thread options
> ber...@fantasyfarm.com writes:
> > I've been chasing a compile problem =A0and I just saw the light on what
> > my trouble is: I use a bunch of "use constant this =3D> ...; use
> > constant that =3D>...;" to set parameters for the program. =A0BUT: the
> > program uses a 'package' or two [it has a small embedded objects in
> > it].

I note that this is documented on the 'constant' man page. Sigh. I
guess I could just use ::THIS and ::that but that feels a bit
inelegant...

> I think you'd rather just export the constants to any package that
> needs them, instead of making them truly global (which would be more
> or less equivalent to exporting them to *all* packages)

Can you export from main *to* a package? Using Exporter in this way
looks like it is a little confusing (it might become clearer after
another six readings of the man page..:o)). I guess I could do
something along the lines of putting require Exporter in my main
program, setting up @EXPORT_OK in the main program the "constants" I
want the interior packages to be able to import, and then do
a ::import(stuff I need) after the 'package' for the interior object-
section? You're right: that kind of approach would make the interior
packages be more self-contained and clearer (by making explicit that
they're pulling in those vbls from "main::")

OTOH, I see that there's a "Readonly" package that looks like it will
do what I was originally trying to do ["Readonly \my $CONST =3D> val"
generates a real cross-package constant]. [although now that I see a
way to do what I originally wanted, I'm not so sure it is as clean a
way to go as I'd like...:(]

Thanks...

/Bernie\

Posted by bernie on May 13, 2008, 10:01 am
Please log in for more thread options
On May 13, 9:50=A0am, ber...@fantasyfarm.com wrote:

> > I think you'd rather just export the constants to any package that
> > needs them, instead of making them truly global (which would be more
> > or less equivalent to exporting them to *all* packages)

>... =A0I guess I could do
> something along the lines of putting require Exporter in my main
> program, setting up @EXPORT_OK in the main program the "constants" I
> want the interior packages to be able to import, and then do
> a ::import(stuff I need) after the 'package' for the interior object-
> section?

I'm running down this path to see how it looks and feels to properly
export and import my "global" constants. And I've run into a
problem. The man page for Exporter says:

package YourModule;
use Exporter 'import'; # gives you Exporter's import() method
directly
@EXPORT_OK =3D qw(munge frobnicate); # symbols to export on request

But in my code I have:
--------------------------
#!/usr/bin/perl

use strict ;
use warnings ;

use Exporter 'import' ;
our @ISA =3D qw(Exporter);
use constant AAA =3D> 4 ;
our @EXPORT_OK =3D ('AAA') ;
----------------------------

"import" is not exported by the Exporter module at test.pl line 6

I can't figure out quite what I'm doing wrong... And then I'm
guessing that down in the program in my package I'd do:
package MYPACKAGE;
::import(qw(constants I want))

/Bernie\

Posted by Ben Morrow on May 13, 2008, 10:26 am
Please log in for more thread options

Quoth bernie@fantasyfarm.com:
> On May 13, 9:50 am, ber...@fantasyfarm.com wrote:
>
> > > I think you'd rather just export the constants to any package that
> > > needs them, instead of making them truly global (which would be more
> > > or less equivalent to exporting them to *all* packages)
>
> >...  I guess I could do
> > something along the lines of putting require Exporter in my main
> > program, setting up @EXPORT_OK in the main program the "constants" I
> > want the interior packages to be able to import, and then do
> > a ::import(stuff I need) after the 'package' for the interior object-
> > section?

It's probably a bad idea to export from main::. I'd define a separate
package for your constants, and 'use' it from everywhere else.

> I'm running down this path to see how it looks and feels to properly
> export and import my "global" constants. And I've run into a
> problem. The man page for Exporter says:
>
> package YourModule;
> use Exporter 'import'; # gives you Exporter's import() method
> directly
> @EXPORT_OK = qw(munge frobnicate); # symbols to export on request
>
> But in my code I have:
> --------------------------
> #!/usr/bin/perl
>
> use strict ;
> use warnings ;
>
> use Exporter 'import' ;
> our @ISA = qw(Exporter);
> use constant AAA => 4 ;
> our @EXPORT_OK = ('AAA') ;
> ----------------------------
>
> "import" is not exported by the Exporter module at test.pl line 6

Does the manpage you're using match your version of Exporter? The
ability to import (rather than inherit) Exporter::import was not present
in older versions of Exporter.

Ben

--
Although few may originate a policy, we are all able to judge it.
Pericles of Athens, c.430 B.C.
ben@morrow.me.uk

Similar ThreadsPosted
Need help with constants and package names. December 12, 2004, 9:01 pm
How can I use constants? July 12, 2008, 8:57 am
Getting constants from *.h header files July 19, 2004, 7:56 pm
SysV constants, where are they defined? November 17, 2004, 10:16 am
Handling constants in Perl? April 19, 2005, 11:40 am
Help with "require" and importing constants September 24, 2006, 5:04 pm
Using named constants in cases of a switch September 6, 2006, 8:07 am
Problem using defined constants as hash keys April 15, 2005, 6:05 am
How can I share common data structures and constants April 18, 2008, 1:04 am
Using a package that might not be available January 6, 2006, 8:55 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap