|
From: Olivier D. <dr...@sh...> - 2002-01-28 21:05:56
|
Hi, I would just like to submit a question to everyone to either answer or debate. Would it be a better programming practice to use subroutines instead of variables as constants? (As my knowledge of Perl is relatively limited, please let me know if this is not backward compatible to 5.004_04) Let me explain why I believe that yes is the right answer. I've been told several times and read it various times as well that a constants is simply a function that takes no arguments and only returns a value. Here's an example taken from formmail: my $emulate_matts_code = 0; my $secure = 1; my $mailprog = '/usr/lib/sendmail -oi -t'; my @referers = qw(dave.org.uk 209.207.222.64 localhost); my @allow_mail_to = qw(yo...@yo... som...@yo... localhost); This should be enough to illustrate my example. Now this would be translated into the following format under the premise: sub EMULATE_MATTS_CODE { 0 } sub SECURE { 1 } sub MAILPROG { '/usr/lib/senmail -oi -t' } sub REFERERS { qw(dave.org.uk 209.207.222.64 localhost) } sub ALLOW_MAIL_TO { qw(yo...@yo... som...@yo... localhost) } (ps, I might be wrong about the behavior of the last two... but you get my point, at least I hope. I've been successfully writing programs using this technique) My main arguments in favor or this: 1. Subroutine values cannot be modified (these are constants right?) 2. Improved code readability ( &CONSTANT instead of $constant or $CONSTANT) which really distinguishes between code variables and constants. 3. Seperate configuration (through constants) and algorithms (that use variables which makes it easier for newbies or people who wish to learn Perl). My main arguments against: 1. Most, if not all Perl programs use scalars, arrays and hashes variables as constants. 2. Might break MWS (although I don't see why if we get the code working right with it). 3. Extra work (in the case where it's done by me, cannot be argued as extra work for others :o) Whaddyatink? -Olivier -- +----------------------------------------------+ | Olivier Dragon dr...@sh... | | Software Engineering II, McMaster University | +----------------------------------------------+ |
|
From: Christopher R. <red...@ma...> - 2002-01-28 21:34:14
|
On Mon, 2002-01-28 at 16:10, Olivier Dragon wrote:
> My main arguments against:
> 1. Most, if not all Perl programs use scalars, arrays and hashes
> variables as constants.
> 2. Might break MWS (although I don't see why if we get the code working
> right with it).
> 3. Extra work (in the case where it's done by me, cannot be argued as
> extra work for others :o)
4. Doesn't interpolate directly.
ie compare:
print "Mail program is $mailprog.\n";
vs.
print "Mail program is ", MAILPROG, ".\n";
or even
print "Mail program is @{[MAILPROG]}.\n";
|
|
From: Jonathan S. <gel...@ge...> - 2002-01-28 21:39:39
|
On Mon, 28 Jan 2002, Olivier Dragon wrote:
> I would just like to submit a question to everyone to either answer or
> debate.
>
> Would it be a better programming practice to use subroutines instead of
> variables as constants? (As my knowledge of Perl is relatively limited,
> please let me know if this is not backward compatible to 5.004_04)
>
It would be backwardly compatible as far as the Perl interpreter is
concerned but I have grave doubts as to whether using this idiom is going
to address our aims. In the first place I really am not going to be the
one who goes through the code altering the places where these variables
are interpolated into quoted strings, which really would mean assigning
their values to new variables rather than some @{[EMULATE_MATTS_CODE()]}
hack ... and then there is the documentation and the support issues :)
/J\
--
Jonathan Stowe |
<http://www.gellyfish.com> | This space for rent
|
|
|
From: Wizard <wi...@ne...> - 2002-01-28 23:19:24
|
> Would it be a better programming practice to use subroutines instead of > variables as constants? In an ideal world, it might. However, there is much more overhead associated with creating and calling a subroutine then using a simple scalar. When you create a subroutine, Perl needs to create a new namespace, stack, etc., and then when called, there is quite a bit of code that needs to be executed in order to pass all of the information (even if there isn't any explicitly passed) to the subroutine. Although your suggestion would work, I think it's a bit of overkill for the sake of saying that it's "better programming practice". Grant M. |
|
From: Olivier D. <dr...@sh...> - 2002-01-28 23:35:30
|
On Mon, Jan 28, 2002 at 06:17:27PM -0800, Wizard wrote: > In an ideal world, it might. However, there is much more overhead associated > with creating and calling a subroutine then using a simple scalar. When you > create a subroutine, Perl needs to create a new namespace, stack, etc., and > then when called, there is quite a bit of code that needs to be executed in > order to pass all of the information (even if there isn't any explicitly > passed) to the subroutine. Would the "use constant CONSTANTNAME => VALUE" be more efficient? As efficient as scalars, arrays or hash? > Although your suggestion would work, I think it's > a bit of overkill for the sake of saying that it's "better programming > practice". Maybe I'm pushing it too far, but I thought the aim of this project was to provide better quality (in delivering a service, in security and as a coding model) In my opinion, allowing what should be constants to be modified isn't a very secure practice. Would you do the same in C? -Oli -- +----------------------------------------------+ | Olivier Dragon dr...@sh... | | Software Engineering II, McMaster University | +----------------------------------------------+ |
|
From: Wizard <wi...@ne...> - 2002-01-29 00:37:00
|
> Would the "use constant CONSTANTNAME => VALUE" be more efficient? As
> efficient as scalars, arrays or hash?
No, all that 'use constant X' does is create an anonymous subroutine 'X'.
It's equivalent to:
*X = sub () { <value> };
which is basically what you are suggesting. 'use constant' also isn't
supported in 5.004x, which I believe is the target.
> Maybe I'm pushing it too far, but I thought the aim of this project was
> to provide better quality (in delivering a service, in security and as
> a coding model) In my opinion, allowing what should be constants to be
> modified isn't a very secure practice. Would you do the same in C?
Provided that the code is written well, it shouldn't be possible to modify
ANY variables that aren't exposed through CGI (and I should hope that only
the ones that are necessary are).
The idea that Perl doesn't really have any constants is an ongoing debate
(and the reason for the 'use constants' hack). I personally believe that
Perl manages just fine without them, and provided the developer uses due
caution in the code, it really is only a issue of resources. As far as C
goes, It's an entirely different language, and not really a practical
comparison (Perl say's "What's a macro?") ;-)
Grant M.
|