Ignorant question regarding includes

D

Debo

Hello there,

I'd like to set up my application so that a whole bunch of global
'constants' are available merely by including the file somehow.

The gist of what I'm trying to do is something like this:


***In constants.pl:

my $constant1 = 'some value';
my $constant2 = 'some other value';

***In somefile.pl:

require constants.pl;

if ('hello' eq $constant1)
{
....
}

This sort of thing is done is C/C++ all the time by including header files
or something hackish like that... that's along the lines of what I'm
trying to do.

I use strict everywhere, so scope will be an issue I imagine. Also, I
don't like to use the constants package because I tend to use these
'constants' in between double/interpolated quotes all the time.

Any suggestions? I'm open to anything that will allow me to store all of
my constant filepaths etc. in a single file. I'm sorry if the way to do
this is obvious in the documentation somewhere, I just haven't been able
to figure it out.

Thanks,

-Debo
 
G

Greg Bacon

: I'd like to set up my application so that a whole bunch of global
: 'constants' are available merely by including the file somehow.
:
: [...]
:
: I use strict everywhere, so scope will be an issue I imagine. Also, I
: don't like to use the constants package because I tend to use these
: 'constants' in between double/interpolated quotes all the time.
:
: Any suggestions? I'm open to anything that will allow me to store all of
: my constant filepaths etc. in a single file. I'm sorry if the way to do
: this is obvious in the documentation somewhere, I just haven't been able
: to figure it out.

How about putting your constants in a module?

$ cat Constants.pm
package Constants;

use strict;

*Constants::constant1 = \'some value';
*Constants::constant2 = \'some other value';

1;

$ cat try
#! /usr/local/bin/perl

use warnings;
use strict;

use Constants;

print "\$Constants::constant1 = $Constants::constant1\n",
"\$Constants::constant2 = $Constants::constant2\n";

$Constants::constant1 = 42;

$ ./try
$Constants::constant1 = some value
$Constants::constant2 = some other value
Modification of a read-only value attempted at ./try line 11.

Note that the trick of creating a constant by assigning a reference to
a typeglob (e.g., C<*Constants::constant1 = \'some value';>) is
documented in the perlmod manpage. Search for PI.

Take this advice with a grain of salt: it may or may not be the best
approach, but that's hard to say because your post focused on the
mechanism rather than the problem you're trying to solve.

Using Perl as though it were a merely a nicer C++ isn't dangerous,
but it's likely to cause you to do a lot more work than necessary and
to miss opportunities to learn new and interesting techniques.

Hope this helps,
Greg
 
D

Debo

GB> Take this advice with a grain of salt: it may or may not be the best
GB> approach, but that's hard to say because your post focused on the
GB> mechanism rather than the problem you're trying to solve.
GB>
GB> Using Perl as though it were a merely a nicer C++ isn't dangerous,
GB> but it's likely to cause you to do a lot more work than necessary and
GB> to miss opportunities to learn new and interesting techniques.

Well, the nature of my problem is fairly simple. I'm writing a series of
cgi scripts and they all need to know about certain filepaths on the
server. I just wanted a place to dump a bunch of constants describing
these filepaths so that I wouldn't have to change a dozen scripts if one
of the paths changed.

You're right that my post concentrated on the mechanism, but that's mostly
because said mechanism is the way I'm accustomed to hacking things :)
(Your method works very well by the way; thanks for that!) However, if
there is a better/cleaner/zanier way to maintain global constants across
multiple files, I'm certainly open to hearing them. I didn't think that
there would be that many ways to do such a thing...

Thanks again!

-Debo
 
G

Greg Bacon

: Well, the nature of my problem is fairly simple. I'm writing a series
: of cgi scripts and they all need to know about certain filepaths on
: the server. I just wanted a place to dump a bunch of constants
: describing these filepaths so that I wouldn't have to change a dozen
: scripts if one of the paths changed.
:
: You're right that my post concentrated on the mechanism, but that's
: mostly because said mechanism is the way I'm accustomed to hacking
: things :) (Your method works very well by the way; thanks for that!)
: However, if there is a better/cleaner/zanier way to maintain global
: constants across multiple files, I'm certainly open to hearing them. I
: didn't think that there would be that many ways to do such a thing...

I'd say what you're doing is reasonable. It sounds like you're making
sound application of the "Keep It Simple, Stupid" rule. :)

Greg
 
D

Debo

GB> I'd say what you're doing is reasonable. It sounds like you're making
GB> sound application of the "Keep It Simple, Stupid" rule. :)

Excellent. I'm going to continue stupiding my way through this
application, then :)

Thanks again for your help.

-Debo
 
T

Tad McClellan

Debo said:
Hello there,

I'd like to set up my application so that a whole bunch of global
'constants' are available merely by including the file somehow.

The gist of what I'm trying to do is something like this:


***In constants.pl:

my $constant1 = 'some value'; ^^
my $constant2 = 'some other value';
^^


Lexical variables are never visible across file boundaries,
for that you need the other kind of variable (package variables).

See also:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html


***In somefile.pl:

require constants.pl;


require requires a string.

You should put quotes around strings.

if ('hello' eq $constant1)

Any suggestions?


Make a proper module and export the variables via Exporter.

Perhaps even better, have a single global hash instead of
a "whole bunch" of individual variables.
 
D

Debo

TM> > ***In constants.pl:
TM> >
TM> > my $constant1 = 'some value';
TM> ^^
TM> > my $constant2 = 'some other value';
TM>
TM> Lexical variables are never visible across file boundaries,
TM> for that you need the other kind of variable (package variables).


Well, that was sort of the crux of my problem in the first place. This was
more of a conceptual example of what I wanted more than anything.


TM> > ***In somefile.pl:
TM> >
TM> > require constants.pl;
TM>
TM>
TM> require requires a string.
TM>
TM> You should put quotes around strings.


Again, I was just illustrating in general what I wanted to do.


TM> Make a proper module and export the variables via Exporter.
TM>
TM> Perhaps even better, have a single global hash instead of
TM> a "whole bunch" of individual variables.

Both sound like very good ideas... my problem with the latter is I get
tired of writing the name of the darn hash all the time :) Laziness is
part of the reason I picked up perl in the first place...

I'm going to look into this Exporter thingie, it sounds like something I
should know about. Thanks very much.

-Debo
 
M

mark.valenti

Assign all your constants in a file and make sure the last single line
of the file is:

1;

You shouldn't have a problem.
 
I

ioneabu

Assign all your constants in a file and make sure the last single line
of the file is:

1;

You shouldn't have a problem.

The problem for the Perl newbie is that they most likely have tried PHP
first where it is normal for one file to include another with an
include statement.

include('myfile.php');

in PHP, the above statement is the equivalent of typing in the contents
of myfile.php in place of the include statement. The same can be done
in C and C++ with the preprocessor, but I'll bet the OP is really
thinking of PHP, because that's most likely what he would have tried
first in web programming.

While you can do the same in Perl with a module, you have to keep in
mind that you have to qualify your variables with the namespace if they
are out of the main namespace (i.e. in another file) unless you export
them. While the Perl way is better, it is not as simple and
straightforward as the PHP way. I think it would be nice to have a
function that just takes the contents of a file and replaces itself
with those contents before compilation. Maybe that's the problem in
Perl, how can you execute the function before compilation? Maybe
eval()?

I like PHPs include function, but it can make for messy, hard to follow
code when over used.

wana
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top