2011-03-26 78 views
4

我在全局函数中使用heredoc中的beestings时遇到了麻烦。运行时会引发错误“Exception: arg2 is not defined”。这里有一个例子:全局函数中的heredoc中的beestings

ruleset a163x59 { 
    meta { 
    name "Beesting in heredoc" 
    description << 
     Demonstrate the error with beestings in global function heredocs 
    >> 
    author "Steve Nay" 
    logging on 
    } 

    dispatch { 
    } 

    global { 
    myFunc = function(arg1, arg2) { 
     firstString = "This is a regular string: #{arg1}. No problem there."; 
     secondString = << 
      This is a heredoc with a beesting: #{arg2}. Problem! 
     >>; 
     secondString; 
    }; 
    } 

    rule first_rule { 
    select when pageview ".*" setting() 
    pre { 
     msg = myFunc("First argument", "Second argument"); 
    } 
    notify("Testing...", msg) with sticky = true; 
    } 
} 

它从来没有抱怨arg1被不确定的,这表明,使用普通的字符串内使用beesting就好了。

有没有我做错了,或者这是一个错误?

+1

奖励积分(无论如何都是虚拟点...)用于发布代码示例。它使得回答问题变得更容易! – TelegramSam 2011-03-26 04:03:05

回答

3

这实际上是一个错误,但有一个解决方法。使用此修改后的代码替换您的函数def:

myFunc = function(arg1, arg2) { 
    firstString = "This is a regular string: #{arg1}. No problem there."; 
    secondString = << 
     This is a heredoc with a beesting: #{arg2}. Problem! 
    >>; 
    "#{secondString}"; 
}; 

请注意,最后一条语句(返回值)是带引号的beesting。这迫使解决heredoc中的任何问题,并且工作。

发生此问题的原因是KRL延迟绑定了beesting替换,直到javascript执行,但在闭包生成中存在导致变量不可用的错误。用引用的beesting强制解决这个问题。

2

我已经在我自己的测试中证实你确实偶然发现了一个错误。我会把它归档,我们会尽快解决这个问题。谢谢。

+0

太棒了。谢谢你的照顾! – 2011-03-26 02:42:50