Subject: | Importing constants slow; especially multiple times |
Importing constants from Net::TacacsPlus::Constants is a little slow,
especially when you need to import them multiple times (i.e. in multiple
packages). You can remove the regular expression name check by just
adding a test to check for typos and you can help keep time down for
multiple imports by creating all the subroutines only once. Attached are
two patches for reference.
Subject: | 0001-Add-test-to-check-there-are-no-typos-in-the-constant.patch |
From f8df0f024329861fdb928851aa55574b046d51cb Mon Sep 17 00:00:00 2001
From: Douglas Christopher Wilson <dougwilson@live.com>
Date: Tue, 16 Nov 2010 01:12:03 -0500
Subject: [PATCH 1/2] Add test to check there are no typos in the constant names
---
t/Net-TacacsPlus-Constant.t | 18 ++++++++++++++++++
1 files changed, 18 insertions(+), 0 deletions(-)
create mode 100644 t/Net-TacacsPlus-Constant.t
diff --git a/t/Net-TacacsPlus-Constant.t b/t/Net-TacacsPlus-Constant.t
new file mode 100644
index 0000000..1f0cd7f
--- /dev/null
+++ b/t/Net-TacacsPlus-Constant.t
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More; # 'no_plan';
+BEGIN { plan tests => 1 };
+
+{
+ package
+ t::constants;
+
+ # Import constants into this clean package
+ use Net::TacacsPlus::Constants;
+
+ # Check all package symbols for typos
+ main::ok(grep { /^TAC_PLUS_/ } keys %t::constants::, 'No constant typos');
+}
--
1.7.3
Subject: | 0002-Create-subs-only-once-and-do-not-check-constant-name.patch |
From 1c8266e80cd430d7e1449ecb2eed924fbd71f66a Mon Sep 17 00:00:00 2001
From: Douglas Christopher Wilson <dougwilson@live.com>
Date: Tue, 16 Nov 2010 01:12:31 -0500
Subject: [PATCH 2/2] Create subs only once and do not check constant names
---
lib/Net/TacacsPlus/Constants.pm | 15 +++++++--------
1 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/lib/Net/TacacsPlus/Constants.pm b/lib/Net/TacacsPlus/Constants.pm
index 848d00b..29daea9 100644
--- a/lib/Net/TacacsPlus/Constants.pm
+++ b/lib/Net/TacacsPlus/Constants.pm
@@ -191,6 +191,12 @@ my %tac_plus_const = (
TAC_PLUS_HEADER_SIZE => 12,
);
+for my $name (keys %tac_plus_const)
+{
+ my $scalar = $tac_plus_const{$name};
+ $tac_plus_const{$name} = sub () { $scalar };
+}
+
=head1 METHODS
=over 4
@@ -206,18 +212,11 @@ sub import {
foreach my $name (keys %tac_plus_const)
{
- #check if we don't import something wrong by mistake to the caller
- unless ($name =~ /^TAC_PLUS_/)
- {
- die('constant "'.$name.'" declaration typoo!');
- }
-
my $fullname="${pkg}::$name";
- my $scalar=$tac_plus_const{$name};
do {
no strict 'refs';
- *$fullname = sub () { $scalar };
+ *$fullname = $tac_plus_const{$name};
}
}
}
--
1.7.3