代码之家  ›  专栏  ›  技术社区  ›  vlad_tepesch

模板工具包:如何从宏块返回散列

  •  0
  • vlad_tepesch  · 技术社区  · 7 年前

    是否可以编写宏或块,将散列返回给调用方?

    我试着模块化一些模板代码:

    [%- 
    MACRO MakeSomeThing(something) BLOCK;
      s = {  a => 'a',
             b => something,
             c => 'c'
          };
      # RETURN s;  # not allowed
      # s;         # just returns the hash ref string (HASH(0x32e42e4))
    END;
    
    
      newOb =  MakeSomeThing('foo');
      dumper.dump({'newOb' => newOb});
    %]
    

    0 回复  |  直到 7 年前
        1
  •  4
  •   ikegami Gilles Quénot    7 年前

    当我面对同样的问题时,我找不到办法。

    作为解决方法,您可以传入引用并让宏修改被引用的变量。这对数组和散列都有效。

    示例定义:

    [%
       # usage: newOb={}; MakeSomeThing(newOb, something)
       MACRO MakeSomeThing(rv, something) BLOCK;
          rv.a = 'a';
          rv.b = something;
          rv.c = 'c';
       END;
    %]
    

    [%
       newOb = {};
       MakeSomeThing(newOb, 'foo');
       dumper.dump({'newOb' => newOb});
    %]