Gossamer Forum
Home : General : Perl Programming :

efficient variables

Quote Reply
efficient variables
i have had separate variables in a config file, such as:
Code:
$admin_notify_add = 1;
$admin_notify_delete = 1;
$admin_notify_modify = 1;
$user_notify_add = 1;
$user_notify_modify = 1;
$record_notify_add = 0;
$record_notify_modify = 0;
those variables are always in memory and available.
i was thinking about changing to something like
Code:
$db_notify = (1,1,1,1,1,0,0)
then when i needed to test, i could use something like
Code:
my ($notify) = (split (/\,/, $db_notify))[1];
if ($notify) { do something, notify admin delete }

which would be more efficient? or is there a better way? thanks!
Quote Reply
Re: [delicia] efficient variables In reply to
Hi,

Shorter code isn't always better - it needs to be "readable" in the future. I would personally do it like:

Code:
my $flags = {
admin_notify_add => 1,
admin_notify_delete => 1,
admin_notify_modify => 1,
user_notify_add => 1,
user_notify_modify => 1,
record_notify_add => 0,
record_notify_modify => 0
};

Then access with:

Code:
$flags->{record_notify_modify}

The beauty of using a hashref, is that if you ever need to debug what setting are currently set, you would just do:

Code:
use Data::Dumper;
print Dumper($flags);

This is so much easier (and cleaner), that having to do something like:

Code:
print "DEBUG: admin_notify_add => $admin_notify_add \n";
print "DEBUG: admin_notify_delete => $admin_notify_delete \n";
print "DEBUG: admin_notify_modify => $admin_notify_modify \n";
print "DEBUG: user_notify_add => $user_notify_add \n";
print "DEBUG: user_notify_modify => $user_notify_modify \n";
print "DEBUG: record_notify_add => $record_notify_add \n";
print "DEBUG: record_notify_modify => $record_notify_modify \n";

Angelic

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!