How can I share common data structures and constants

S

Sean

Hi,

I use certain constants defined in has, array and simple variables over and
over in some of the modules I have written. I was wondering if there was a
"C like include file" in Perl.

Any help is greatly appreciated.

-Sean
 
S

Skye Shaw!@#$

Hi,

I use certain constants defined in has, array and simple variables over and
over in some of the modules I have written. I was wondering if there was a
"C like include file" in Perl.

Any help is greatly appreciated.

-Sean


perldoc -f require

require is similar to include; well maybe a little smarter...

Though I think it's generally better to give them a namespace via a
package.

Like so:

package Ass::Bass;

our $GOOD_YEAR = 16;
our $CASS = 'what it izzz!';

1;

then use them like so:

use Ass::Bass;

print "Sorry franky... $Ass::Bass::GOOD_YEAR"


check out
perldoc -f package
perldoc -f our

-Skye
 
R

rthangam

Hi,

I use certain constants defined in has, array and simple variables over and
over in some of the modules I have written. I was wondering if there was a
"C like include file" in Perl.

Any help is greatly appreciated.

-Sean

You can create a separate module for having all the constant values
like for example:

% cat Const.pm

package Const;

use constant
{
foo => 10,
bar => 23
};

1;


% cat const_test.pl
#! /usr/bin/perl
use Const;

print Const::foo;

This is one way i could think of there should be other ways too
 
R

rthangam

Hi,

I use certain constants defined in has, array and simple variables over and
over in some of the modules I have written. I was wondering if there was a
"C like include file" in Perl.

Any help is greatly appreciated.

-Sean

You can have the constants as a hash in another module. Like for eg.,

% cat Const.pm

package Const;

use constant
{
foo => 10,
bar => 23
};

1;


% cat const_test.pl
#! /usr/bin/perl
use Const;

print Const::foo;

Here Const.pm has the constants which can be used by other files or
modules and also makes it easier to maintain
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top