Need advice on include-like file for Perl

Discussion in 'Perl Misc' started by Amanda aman_DO_da345@al_THE_ter_OBVIOUS_n.org, Apr 14, 2004.

  1. Hi!

    I've done most of my programming in C/C++ and a little in Perl,
    though until recently I have stayed away from Perl OOP. Now I'm
    writing my first suite of classes in Perl, and I wonder how best
    to set up something analogous to an include file in C/C++ consisting
    of definitions of constants to be used by all the classes in this
    suite. The main class in this suite is called SMS. My idea was
    to create a file SMS_Constants.pm to collect all these constants:

    # file SMS_Constants.pm
    package SMS; # keep "prefix" short
    our $Constant_1 = 'some value';
    our $Constant_2 = 'another value';
    our $Constant_3 = 'you get the picture';
    # etc.
    1;

    so that other code could access these constants as $SMS::Constant_1,
    etc. after first invoking "use SMS_Constants.pm" in a spirit similar
    C/C++ programs' use of '# include "some_constants.h"'.

    Is this what's usually done in Perl? Are there any obvious problems
    with this approach? I suppose that TMTOWTDI, but it would be
    instructive to read who different Perl programmers go about doing
    this.

    Many thanks in advance!

    Amanda
    --
     
    Amanda aman_DO_da345@al_THE_ter_OBVIOUS_n.org, Apr 14, 2004
    #1
    1. Advertising

  2. Amanda aman_DO_da345@al_THE_ter_OBVIOUS_n.org wrote:
    [...]
    > writing my first suite of classes in Perl, and I wonder how best
    > to set up something analogous to an include file in C/C++

    [...]

    Depending upon the details of what you want to do
    perldoc -f use
    perldoc -f require
    perldoc -f do

    jue
     
    Jürgen Exner, Apr 14, 2004
    #2
    1. Advertising

  3. Amanda aman_DO_da345@al_THE_ter_OBVIOUS_n.org

    Tore Aursand Guest

    On Wed, 14 Apr 2004 03:45:52 +0000, wrote:
    > I wonder how best to set up something analogous to an include file in
    > C/C++ consisting of definitions of constants to be used by all the
    > classes in this suite. The main class in this suite is called SMS. My
    > idea was to create a file SMS_Constants.pm to collect all these
    > constants:


    Excellent. It's always a good idea to keep constants and common variables
    in one place.

    But why keep it in a module? Why not take a look at one of the modules at
    CPAN [1] which lets you have these values in configuration files?

    [1] <http://www.cpan.org/>

    > # file SMS_Constants.pm
    > package SMS; # keep "prefix" short


    Well. If you're to call the file 'SMS_Constants.pm', you should also call
    the package 'SMS_Constants'. Or maybe the constants thing is a part of
    the SMS namespace? In that case, you should call your package
    'SMS::Constants';

    package SMS::Constants;
    use strict;
    use warnings;

    > our $Constant_1 = 'some value';
    > our $Constant_2 = 'another value';
    > our $Constant_3 = 'you get the picture';


    Funny names for variables, don't you think? Take a look at the use of
    "real" constants in Perl;

    perldoc constant

    > Is this what's usually done in Perl?


    Genereally: To find out how things are usually done in Perl, download
    some of the modules from CPAN and consult them.


    --
    Tore Aursand <>
    "There are three kinds of lies: lies, damn lies, and statistics." --
    Benjamin Disraeli
     
    Tore Aursand, Apr 14, 2004
    #3
  4. Amanda aman_DO_da345@al_THE_ter_OBVIOUS_n.org

    Amanda Guest

    In <> Tore Aursand <> writes:

    >On Wed, 14 Apr 2004 03:45:52 +0000, wrote:
    >> I wonder how best to set up something analogous to an include file in
    >> C/C++ consisting of definitions of constants to be used by all the
    >> classes in this suite. The main class in this suite is called SMS. My
    >> idea was to create a file SMS_Constants.pm to collect all these
    >> constants:


    >Excellent. It's always a good idea to keep constants and common variables
    >in one place.


    >But why keep it in a module? Why not take a look at one of the modules at
    >CPAN [1] which lets you have these values in configuration files?


    Well, these are not user-configurable parameters, so I saw no
    particular benefit in putting them in a separate configuration
    file. They are constants such as pi, pi/2, sqrt(2), log(10),
    Boltzmann's constant, standard error messages, standard urls, etc.

    Thanks,

    Amanda
    --
     
    Amanda, Apr 14, 2004
    #4
  5. Amanda wrote:

    > Hi!
    >
    > I've done most of my programming in C/C++ and a little in Perl,
    > though until recently I have stayed away from Perl OOP. Now I'm
    > writing my first suite of classes in Perl, and I wonder how best
    > to set up something analogous to an include file in C/C++ consisting
    > of definitions of constants to be used by all the classes in this
    > suite. The main class in this suite is called SMS. My idea was
    > to create a file SMS_Constants.pm to collect all these constants:
    >
    > # file SMS_Constants.pm
    > package SMS; # keep "prefix" short
    > our $Constant_1 = 'some value';
    > our $Constant_2 = 'another value';
    > our $Constant_3 = 'you get the picture';
    > # etc.
    > 1;
    >
    > so that other code could access these constants as $SMS::Constant_1,
    > etc. after first invoking "use SMS_Constants.pm" in a spirit similar
    > C/C++ programs' use of '# include "some_constants.h"'.
    >
    > Is this what's usually done in Perl? Are there any obvious problems
    > with this approach? I suppose that TMTOWTDI, but it would be
    > instructive to read who different Perl programmers go about doing
    > this.
    >
    > Many thanks in advance!
    >
    > Amanda


    I often have a package like:

    package SMS::Constants;
    use strict;
    # use warnings; # when developing
    use base qw/Exporter/;
    use vars qw/@EXPORT_OK %EXPORT_TAGS/;

    BEGIN {
    @EXPORT_OK = qw/THIS THAT OTHER/;
    %EXPORT_TAGS = (ALL => [qw/THIS THAT OTHER/],
    STARTS_WITH_TH => [qw/THIS THAT/]);
    Exporter::export_ok_tags(qw/ALL STARTS_WITH_TH/);
    }

    use constant THIS => 'this value';
    use constant THAT => 'that value';
    use constant OTHER => 'other value';

    1;
    __END__

    So you can now do:

    use SMS::Constants qw/THIS/; # Imports THIS
    use SMS::Constants qw/:STARTS_WITH_TH/; # Imports THIS and THAT

    MB
     
    Matthew Braid, Apr 14, 2004
    #5
  6. Amanda aman_DO_da345@al_THE_ter_OBVIOUS_n.org

    Tore Aursand Guest

    On Wed, 14 Apr 2004 04:59:28 +0000, Amanda wrote:
    >> But why keep it in a module? Why not take a look at one of the modules
    >> at CPAN which lets you have these values in configuration files?


    > Well, these are not user-configurable parameters, so I saw no particular
    > benefit in putting them in a separate configuration file.


    But you _did_ that, didn't you? :) My point is that it's generally a good
    idea to keep pre-defined variables (and constants) away from the Perl
    code.

    But - as always - I guess it's a matter of taste, really.


    --
    Tore Aursand <>
    "What we see depends mainly on what we look for." -- Sir John Lubbock
     
    Tore Aursand, Apr 14, 2004
    #6
    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. =?Utf-8?B?Sm9l?=
    Replies:
    1
    Views:
    1,736
    Karl Seguin
    Jan 28, 2005
  2. Victor Bazarov

    #include "file" -vs- #include <file>

    Victor Bazarov, Mar 5, 2005, in forum: C++
    Replies:
    4
    Views:
    550
    Exits Funnel
    Mar 6, 2005
  3. PTM
    Replies:
    1
    Views:
    340
    Andy Dingley
    Nov 12, 2007
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    960
    Andreas Bogenberger
    Feb 22, 2008
  5. me
    Replies:
    10
    Views:
    987
    ccc31807
    Mar 26, 2010
Loading...

Share This Page