Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need advice on include-like file for Perl

Reply
Thread Tools

Need advice on include-like file for Perl

 
 
Amanda aman_DO_da345@al_THE_ter_OBVIOUS_n.org
Guest
Posts: n/a
 
      04-14-2004





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
--
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-14-2004
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



 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      04-14-2004
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 <(E-Mail Removed)>
"There are three kinds of lies: lies, damn lies, and statistics." --
Benjamin Disraeli
 
Reply With Quote
 
Amanda
Guest
Posts: n/a
 
      04-14-2004
In <(E-Mail Removed)> Tore Aursand <(E-Mail Removed)> 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
--
 
Reply With Quote
 
Matthew Braid
Guest
Posts: n/a
 
      04-14-2004
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

 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      04-14-2004
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 <(E-Mail Removed)>
"What we see depends mainly on what we look for." -- Sir John Lubbock
 
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
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Need some advice about file extension of the include file =?Utf-8?B?Sm9l?= ASP .Net 1 01-28-2005 02:27 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments