Exporting Constants in Perl

Devin Whalen devin-Gq53QDLGkWIleAitJ8REmdBPR1lH4CV8 at public.gmane.org
Tue Jun 14 17:21:37 UTC 2005


On Tue, 2005-06-14 at 13:05 -0400, Devin Whalen wrote:
> Hey,
> 
> I have a module in Perl that has some constants that I want to export.
> What is the proper syntax to do this?
> 
> Here is what I am doing:
> 
> package res_commandline;
> 
> BEGIN
> {
>         use Exporter ();
>         use vars   qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
>         # Set the Version Number of this Package for
>         # Version Checking
>         $VERSION = 0.01;
>         @ISA    = qw(Exporter);
>         #  Exported Routines below, with optional tags.
>         @EXPORT = qw();
>         %EXPORT_TAGS= (@EXPORT_OK);
>         # Exported Global Variables. This at the minute should be none.
>         @EXPORT_OK = qw( &logError &getDBParams &programCrash
> &getEnvHash
> TL_LOG_LOG TL_LOG_INFO TL_LOG_NOTICE TL_LOG_WARNING TL_LOG_ERROR
> TL_LOG_CRASH
>  );
> 
> }
> 
> use subs @EXPORT_OK;
> 
> #Global Variables
> my $ERROR_LOG_DIR = "/var/log/talaria";
> my $ERROR_LOG = "error_log";
> 
> use constant TL_LOG_LOG => 55;
> use constant TL_LOG_INFO => 2;
> use constant TL_LOG_NOTICE => 3;
> use constant TL_LOG_WARNING => 4;
> use constant TL_LOG_ERRROR => 5;
> use constant TL_LOG_CRASH => 6;
> 
> #more module code here...
> 
> 
> Then I do this in my file to use it:
> 
> 
> #!/usr/bin/perl -w
> 
> use strict;
> use DBI;
> 
> #import my functions and constants
> use res_commandline qw(logError getDBParams programCrash TL_LOG_LOG
> TL_LOG_INFO TL_LOG_NOTICE TL_LOG_WARNING TL_LOG_ERROR TL_LOG_CRASH);
> 
> 
> use res_utils qw(doSQL); #import my sql function
> 
> 
> print ("TL_LOG_LOG:".TL_LOG_LOG."\n");  #print out my constant
> 
> 
> When I run this, it runs correctly but I get the following error
> messages:
> Prototype mismatch: sub res_commandline::TL_LOG_LOG vs ()
> at /usr/local/lib/perl5/5.6.1/constant.pm line 91.
> Prototype mismatch: sub res_commandline::TL_LOG_INFO vs ()
> at /usr/local/lib/perl5/5.6.1/constant.pm line 91.
> Prototype mismatch: sub res_commandline::TL_LOG_NOTICE vs ()
> at /usr/local/lib/perl5/5.6.1/constant.pm line 91.
> Prototype mismatch: sub res_commandline::TL_LOG_WARNING vs ()
> at /usr/local/lib/perl5/5.6.1/constant.pm line 91.
> Prototype mismatch: sub res_commandline::TL_LOG_CRASH vs ()
> at /usr/local/lib/perl5/5.6.1/constant.pm line 91.
> 
> 
> Is this because perl doesn't know that I am exporting constants?  Is
> there a symbol I have to use, you know like $ for scalars or & for subs?
> I have been looking through my books on perl and the web but I cannot
> find a specific example of exporting a constant.
> 
> Thanks for any help.
> 
> 
> 

I just tried it with "our" instead of "use subs" and it works...but why?

use subs @EXPORT_OK;
becomes
our @EXPORT_OK;

I assume the problem is that use subs only expects subs to be exported.
So should I always use our?  Does use subs offer any benefits over our?
Now my file works, but I just want to know what is the correct syntax
and why.

Thanks.


-- 
Devin Whalen
Programmer
Synaptic Vision Inc
Phone-(416) 539-0801
Fax- (416) 539-8280
543 Richmond Street West
Toronto, Ontario
Suite 223 M5V 1Y6
Box 105
Home-(416) 653-3982


Take back the Web with FireFox....a browser you can trust
www.getfirefox.com

   .-.
   /v\    L   I   N   U   X
  // \\  
 /(   )\
  ^^-^^   

--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list