在使用它之前,如何检查我是否有Perl模块? [英] How can I check if I have a Perl module before using it?

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

问题描述

我有以下依赖于Term::ReadKey的Perl代码来获取终端宽度;我的NetBSD构建缺少此模块,因此当模块丢失时,我想将终端的宽度默认设置为80.

我无法弄清楚如何有条件地使用模块,因此要提前知道它是否可用.我当前的实现只是退出,并显示一条消息,提示它找不到Term::ReadKey.

#/usr/pkg/bin/perl -w
# Try loading Term::ReadKey
use Term::ReadKey;
my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
my @p=(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97);
my $plen=$#p+1;
printf("num |".("%".int(($wchar-5)/$plen)."d") x $plen."\n",@p);

我在NetBSD上使用Perl 5.8.7,在CygWin上使用5.8.8 您可以帮助我将其更有效地实现到脚本中吗?

解决方案

这是不需要其他模块的准系统解决方案:

my $rc = eval
{
  require Term::ReadKey;
  Term::ReadKey->import();
  1;
};

if($rc)
{
  # Term::ReadKey loaded and imported successfully
  ...
}

请注意,下面使用eval { use SomeModule }的所有答案(我希望它们都低于该答案!:-)都是错误的,因为use语句是在编译时求值的,而不管它们出现在代码中的什么位置.因此,如果SomeModule不可用,该脚本将在编译后立即终止.

(use语句的字符串eval也将起作用(eval 'use SomeModule';),但是当require/import对执行相同的操作时,没有意义在运行时解析和编译新代码,并且在编译时进行语法检查以启动.)

最后,请注意,出于本示例的目的,我在这里对eval { ... }$@的使用是简洁的.在真实代码中,您应该使用 Try :: Tiny 之类的东西,或者至少使用请注意其要解决的问题.

I have the following Perl code which relies on Term::ReadKey to get the terminal width; My NetBSD build is missing this module, so I want to default the width of the terminal to 80 when the module is missing.

I can't figure out how to conditionally use a module, knowing ahead of time whether it is available. My current implementation just quits with a message saying it can't find Term::ReadKey if it's absent.

#/usr/pkg/bin/perl -w
# Try loading Term::ReadKey
use Term::ReadKey;
my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
my @p=(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97);
my $plen=$#p+1;
printf("num |".("%".int(($wchar-5)/$plen)."d") x $plen."\n",@p);

I'm using Perl 5.8.7 on NetBSD and 5.8.8 on CygWin Can you help me implement this into my script more effectively?

解决方案

Here's a bare-bones solution that does not require another module:

my $rc = eval
{
  require Term::ReadKey;
  Term::ReadKey->import();
  1;
};

if($rc)
{
  # Term::ReadKey loaded and imported successfully
  ...
}

Note that all the answers below (I hope they're below this one! :-) that use eval { use SomeModule } are wrong because use statements are evaluated at compile time, regardless of where in the code they appear. So if SomeModule is not available, the script will die immediately upon compiling.

(A string eval of a use statement will also work (eval 'use SomeModule';), but there's no sense parsing and compiling new code at runtime when the require/import pair does the same thing, and is syntax-checked at compile time to boot.)

Finally, note that my use of eval { ... } and $@ here is succinct for the purpose of this example. In real code, you should use something like Try::Tiny, or at least be aware of the issues it addresses.

这篇关于在使用它之前,如何检查我是否有Perl模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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