If my Perl code has a production code location and "beta" code location
(e.g. production Perl code us in /usr/code/scripts
, BETA Perl code is in /usr/code/beta/scripts
; production Perl libraries are in /usr/code/lib/perl
and BETA versions of those libraries are in /usr/code/beta/lib/perl
, is there an easy way for me to achieve such a setup?
The exact requirements are:
The code must be THE SAME in production and BETA location.
To clarify, to promote any code (library or script) from BETA to production, the ONLY thing which needs to happen is literally issuing cp
command from BETA to prod location - both the file name AND file contents must remain identical.
BETA versions of scripts must call other BETA scripts and BETA libraries (if exist) or production libraries (if BETA libraries do not exist)
The code paths must be the same between BETA and production with the exception of base directory (/usr/code/
vs /usr/code/beta/
)
The scripts must be all under the same base directory but they may be in its sub-directories at arbitrary depth level (this precludes the classic use lib "$FindBin::Bin/../lib"
solution from section 31.13. use lib of "Programming Perl")
I will present how we solved the problem as an answer to this question, but I'd like to know if there's a better way.
Our own solution was as follows:
Have a library (let's call it BetaOrProd.pm)
use BetaOrProd;
" in every script
use
statement in every script after "use strict;
" pragma (and "use warnings" if we use that). Including before any BEGIN
blocks.BEGIN
block which contains most of the logicBEGIN
block in the library checks the program's directory path (based off of $0 with absolute path applied)/usr/code/beta
, the program is deemed to run in BETA location, else in production/usr/local/lib/perl
is un-shifted to the beginning of @INC
list/usr/code/beta/lib/perl
is un-shifted to the beginning of @INC
list after that.Anytime a script/library needs to call another script, the path to the called script is calculated based on said accessor to $isBETA variable exported from BetaOrProd.pm
Anytime a Perl library needs to be required or used, no special logic is needed - the @INC
modified by BetaOrProd.pm takes care of knowing where the modules are to be imported from. If the module is present in BETA location, then the library from BETA location will be used by BETA script, else the library from prod location.
The main drawbacks of this approach are:
Requirement that every script must have "use BetaOrProd;
" as the very first use
statement in every script after "use strict;
" pragma.
Mitigated by the fact that our company requires every deployed piece of code to pass automated validator, which can check for this requirement.
Can't BETA test BetaOrProd.pm via /usr/code/beta/lib/perl
. D'uh.
Mitigated by very thorough unit and integration test of the library
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With