列出已定义的 vmethod 似乎不起作用

lists defined-vmethod seems not to work

在不同的场合我运行进入这个问题。

我想测试列表中是否定义了某些值,但列表中只有 vmethod returns false;

例如在这种情况下:

  lines = "polyline";
  validLineOptions = ['line', 'polyline', 'curved', 'ortho', 'spline'];
  IF validLineOptions.defined(lines);
    GET( "/* using style " _ lines _ "*/\n");
  ELSE;
    GET( "/* using default style */\n");
    lines = '';
  END;  

这始终在 ELSE 子句中运行。

一些版本信息:

This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x86-multi-thread-64int

  Wed May 31 02:57:08 2017: "Module" Template
    *   "installed into: C:\strawberry\perl\vendor\lib"
    *   "LINKTYPE: dynamic"
    *   "VERSION: 2.27"
    *   "EXE_FILES: bin/tpage bin/ttree"

您不能使用列表元素的值来查找它。

这就是 the documentationlists(TT 中的数组)上关于 defined 的说法。

Returns a true or false value if the item in the list denoted by the argument is defined.

[% list.defined(3) ? 'yes' : 'no' %]

When called without any argument, list.defined returns true if the list itself is defined (e.g. the same effect as scalar.defined).

然而,这就是你所做的。

lines = "polyline";
validLineOptions.defined(lines)

这使用带有字符串值的变量lines作为列表的索引。索引应该是一个数字,因为我们不是在处理 hash(关联数组)。此列表中的值已排序并编号。您不能按值访问它们。

我认为您要做的是检查该值是否存在于列表中。有几种方法可以做到这一点。

validLineOptions.grep(lines)

使用grep查找数组中的元素并检查返回值的个数是否为真。如果没有匹配,则 returns false。

但是,您也可以使用哈希作为查找。

lines = "polyline";
validLineOptions = {
    'line'     = 1, 
    'polyline' = 1, 
    'curved'   = 1, 
    'ortho'    = 1, 
    'spline'   = 1
};
IF validLineOptions.exists(lines);

这将检查散列 中的 key 是否存在 。这是检查值是否被允许的简单方法。