You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by allan juul <la...@inet.uni2.dk> on 2002/07/28 15:45:14 UTC

Using Constants from seperate file

hi

i wish to use use constants defined in a seperate file. my problem is, that 
sometimes it works sometimes it doesn't. when failing, the error_log says 
Bareword is not allowed so i guess it sometimes never loads my constants file 
at all.

this is my basic approach:


Package Foo:Bar;

use strict;

BEGIN {
  require "Constants.lib";
}


sub handler {
  my $r = shift;
  ...

  my $sql = SQL_SELECT; # pull in constant value
  ....

}


is this a stupid approach, and if so what would be a more correct solution?
(i would prefer _not_ to have the constant file loaded once and for all in a 
startup.pl).


any suggestions appriciated
thanks
./allan

Re: Using Constants from seperate file

Posted by Steve Piner <st...@marketview.co.nz>.
What does 'Constants.lib' look like? Does it have a 'package' statement?
Does it use Exporter?

If it's a package then unless you've got some magic in 'Constants.lib',
that BEGIN block will not pull the constants into the current package -
the 'import' call is missing. (See perldoc -f use)

If it isn't a package, then it will only get pulled into the first
package that 'require's it. In this case you probably want to be using
'do "file"' rather than 'require', however it would probably be better
to convert the file into a proper package.

What's so terrible about loading the constants file at startup?


Steve Piner


allan juul wrote:
> 
> hi
> 
> i wish to use use constants defined in a seperate file. my problem is, that
> sometimes it works sometimes it doesn't. when failing, the error_log says
> Bareword is not allowed so i guess it sometimes never loads my constants file
> at all.
> 
> this is my basic approach:
> 
> Package Foo:Bar;
> 
> use strict;
> 
> BEGIN {
>   require "Constants.lib";
> }
> 
> sub handler {
>   my $r = shift;
>   ...
> 
>   my $sql = SQL_SELECT; # pull in constant value
>   ....
> 
> }
> 
> is this a stupid approach, and if so what would be a more correct solution?
> (i would prefer _not_ to have the constant file loaded once and for all in a
> startup.pl).
> 
> any suggestions appriciated
> thanks
> ./allan

-- 
Steve Piner
Web Applications Developer
Marketview Limited
http://www.marketview.co.nz

Re: Using Constants from seperate file

Posted by Ged Haywood <ge...@www2.jubileegroup.co.uk>.
Hi there,

On Sun, 28 Jul 2002, allan juul wrote:

> i wish to use use constants defined in a seperate file. my problem is, that 
> sometimes it works sometimes it doesn't. when failing, the error_log says 
> Bareword is not allowed so i guess it sometimes never loads my constants file 
> at all.
> 
> this is my basic approach:
> 
> 
> Package Foo:Bar;
> 
> use strict;
> 
> BEGIN {
>   require "Constants.lib";
> }
> 
> 
> sub handler {
>   my $r = shift;
>   ...
> 
>   my $sql = SQL_SELECT; # pull in constant value
>   ....
> 
> }
> 
> 
> is this a stupid approach,

Only under some circumstances.  Check out the mod_perl Guide.  If I
were debugging this I'd put a "PRINT STDERR 'something';" statement in
the BEGIN block.  Probably that will tell you what's happening...

73,
Ged.