Need advice on include-like file for Perl

  • Thread starter Amanda aman_DO_da345
  • Start date
A

Amanda aman_DO_da345

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

Jürgen Exner

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
 
T

Tore Aursand

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?

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

Amanda

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

Matthew Braid

Amanda said:
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
 
T

Tore Aursand

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top