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