How can I share common data structures and constants

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
Sean [ Fr, 18 April 2008 07:04 ] [ ID #1945477 ]

Re: How can I share common data structures and constants

Sean wrote:
>
> 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.

Declare the variables with our and include the
file with use or require.

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
Frank Seitz [ Fr, 18 April 2008 08:49 ] [ ID #1945480 ]

Re: How can I share common data structures and constants

On Apr 17, 10:04=A0pm, "Sean" <imfeaw5... [at] pacbell.net> wrote:
> Hi,
>
> I use certain constants defined in has, array and simple variables over an=
d
> 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 =3D 16;
our $CASS =3D '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
skye.shaw [ Fr, 18 April 2008 08:56 ] [ ID #1945481 ]

Re: How can I share common data structures and constants

"Sean" <imfeaw5672 [at] pacbell.net> writes:

> 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.

Yes - it's called modules. Given that you've already written some, I don't
understand what you're really asking here. What's stopping you from simply
placing your constants, etc. in a module and use()-ing it?

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Sherm Pendley [ Fr, 18 April 2008 09:08 ] [ ID #1945483 ]

Re: How can I share common data structures and constants

On Apr 18, 10:04 am, "Sean" <imfeaw5... [at] pacbell.net> wrote:
> 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
ramesh.thangamani [ Fr, 18 April 2008 09:39 ] [ ID #1945485 ]

Re: How can I share common data structures and constants

On Apr 18, 10:04 am, "Sean" <imfeaw5... [at] pacbell.net> wrote:
> 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
ramesh.thangamani [ Fr, 18 April 2008 09:42 ] [ ID #1945486 ]
Perl » comp.lang.perl.misc » How can I share common data structures and constants

Vorheriges Thema: Regx to remove all characters after a match
Nächstes Thema: Perl - delayed evaluation of variables?