Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How can I share common data structures and constants

Reply
Thread Tools

How can I share common data structures and constants

 
 
Sean
Guest
Posts: n/a
 
      04-18-2008
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


 
Reply With Quote
 
 
 
 
Skye Shaw!@#$
Guest
Posts: n/a
 
      04-18-2008
On Apr 17, 10:04*pm, "Sean" <(E-Mail Removed)> 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



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
 
Reply With Quote
 
 
 
 
rthangam
Guest
Posts: n/a
 
      04-18-2008
On Apr 18, 10:04 am, "Sean" <(E-Mail Removed)> 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
 
Reply With Quote
 
rthangam
Guest
Posts: n/a
 
      04-18-2008
On Apr 18, 10:04 am, "Sean" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
common data structures max(01)* Python 1 07-31-2004 09:19 PM



Advertisments