如何使用Beta Perl脚本中的Beta Perl模块? [英] How do I use beta Perl modules from beta Perl scripts?

查看:88
本文介绍了如何使用Beta Perl脚本中的Beta Perl模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的Perl代码具有生产代码位置和"beta"代码位置 (例如,我们在/usr/code/scripts中使用生产Perl代码,在/usr/code/beta/scripts中使用BETA Perl代码;在/usr/code/lib/perl中使用生产Perl库,而这些库的BETA版本在/usr/code/beta/lib/perl中,对我而言,是否有一种简单的方法可以实现设置?

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 code must be THE SAME in production and BETA location.

为了明确说明,要将任何代码(库或脚本)从BETA推广到生产环境,唯一需要做的就是从BETA实际将cp命令发布到产品位置-文件名和文件内容必须保持相同.

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版本的脚本必须调用其他BETA脚本和BETA库(如果存在)或生产库(如果BETA库不存在)

BETA versions of scripts must call other BETA scripts and BETA libraries (if exist) or production libraries (if BETA libraries do not exist)

BETA和生产之间的代码路径必须相同,除了基本目录(/usr/code//usr/code/beta/)

The code paths must be the same between BETA and production with the exception of base directory (/usr/code/ vs /usr/code/beta/)

脚本必须全部位于同一基本目录中,但它们可以位于任意深度级别的子目录中(这排除了部分中的经典use lib "$FindBin::Bin/../lib"解决方案31.13.使用"Programming Perl" )

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:

  • 拥有一个库(我们将其称为BetaOrProd.pm)

  • Have a library (let's call it BetaOrProd.pm)

  • 必须通过每个脚本
  • 中的"use BetaOrProd;"将库包括在内
  • 在每个脚本中,库必须是"use strict;"编译后的第一个use语句(如果使用,则为使用警告").包括在任何BEGIN块之前.
  • 该库有一个BEGIN块,其中包含大多数逻辑
  • 库中的BEGIN块检查程序的目录路径(基于$ 0并应用了绝对路径)
  • 如果目录路径以/usr/code/beta开头,则该程序将在BETA位置运行,否则将在生产环境中运行
  • 无论哪种情况,/usr/local/lib/perl都不会移到@INC列表的开头
  • 如果是测试版位置,则/usr/code/beta/lib/perl会在此之后移至@INC列表的开头.
  • 如果BETA位置,则将特殊变量$ isBETA(可从BetaOrProd.pm导出的访问器方法访问)设置为"BETA".
  • The library MUST be included via "use BetaOrProd;" in every script
  • The library MUST be the very first use statement in every script after "use strict;" pragma (and "use warnings" if we use that). Including before any BEGIN blocks.
  • The library has a BEGIN block which contains most of the logic
  • That BEGIN block in the library checks the program's directory path (based off of $0 with absolute path applied)
  • If the directory path starts with /usr/code/beta, the program is deemed to run in BETA location, else in production
  • In either case, /usr/local/lib/perl is un-shifted to the beginning of @INC list
  • If BETA location, /usr/code/beta/lib/perl is un-shifted to the beginning of @INC list after that.
  • If BETA location, a special variable $isBETA (accessible by a accessor method exported from BetaOrProd.pm) is set to "BETA".

每当一个脚本/库需要调用另一个脚本时,将根据从BetaOrProd.pm导出的$ isBETA变量的所述访问器来计算被调用脚本的路径

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

每当需要或使用Perl库时,都不需要特殊的逻辑-BetaOrProd.pm修改的@INC负责知道从何处导入模块.如果模块位于BETA位置,则BETA脚本将使用BETA位置的库,否则prod位置的库将使用该库.

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:

  1. 要求每个脚本都必须在"use strict;"编译指示之后的每个脚本中,都将"use BetaOrProd;"作为每个脚本的第一个use语句.

  1. 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.

无法通过/usr/code/beta/lib/perl测试BetaOrProd.pm. '.

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

这篇关于如何使用Beta Perl脚本中的Beta Perl模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆