How can I share common data structures and constants

Discussion in 'Perl Misc' started by Sean, Apr 18, 2008.

  1. Sean

    Sean Guest

    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, Apr 18, 2008
    #1
    1. Advertising

  2. On Apr 17, 10:04 pm, "Sean" <> 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
     
    Skye Shaw!@#$, Apr 18, 2008
    #2
    1. Advertising

  3. Sean

    rthangam Guest

    On Apr 18, 10:04 am, "Sean" <> 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
     
    rthangam, Apr 18, 2008
    #3
  4. Sean

    rthangam Guest

    On Apr 18, 10:04 am, "Sean" <> 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
     
    rthangam, Apr 18, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Garth17
    Replies:
    3
    Views:
    3,052
    Brock Allen
    Mar 18, 2005
  2. max(01)*

    common data structures

    max(01)*, Jul 31, 2004, in forum: Python
    Replies:
    1
    Views:
    236
    Marc 'BlackJack' Rintsch
    Jul 31, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    728
    Emmanuel Delahaye
    Sep 24, 2005
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,369
    Saraswati lakki
    Jan 6, 2012
  5. John Reye
    Replies:
    28
    Views:
    1,388
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page