Perl 导入到我所有的包中? [英] Perl import into all my packages?

查看:63
本文介绍了Perl 导入到我所有的包中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我倾向于经常使用 Data::Dumper,最终在我的 .pl 代码中的每个包的顶部都有以下样板.

I tend to use Data::Dumper very often, and I end up having the following boilerplate at the top of every package in my .pl code.

use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Deparse = 1;
$Data::Dumper::Purity = 1;

有没有办法指定在当前的 .pl 文件中,我希望自动假设这些语句."

Is there a way to specify that "inside the current .pl file, I want these statements to automatically assumed."

例如我会

package foo;
    use strict;
    use warnings;
    use Data::Dumper;
    $Data::Dumper::Deparse = 1;
    $Data::Dumper::Purity = 1; 

    my @localState = (1, 2, 3, 4, 5);

    sub test {
        print Dumper \@localState;
    }

package main;
    use strict;
    use warnings;
    use Data::Dumper;
    $Data::Dumper::Deparse = 1;
    $Data::Dumper::Purity = 1;

    foo->test;

这会很快导致过多的样板重复并损害可维护性.

this can quickly get way too much boilerplate repetition and harm maintainability.

遗憾的是,我不能使用带有eval"的函数来调用所有这些样板,因为该样板将放置在函数中,而不是全局作用域中;Perl 没有 Lisp 宏,我知道它实际上具有非函数绑定的 eval 式行为(我可能错了,如果 Perl 有 lisp 宏会很酷).

Sadly I can't use a function with "eval" in it to call all this boilerplate since that boilerplate would be placed into the function, not global scope; Perl does not have Lisp macros that I know of to actually have non-function bound eval-like bahavior(I could be wrong, would be so cool if Perl had lisp macros).

如果它是在文件中声明的第一个包,有没有人知道这种行为是否可以在不编写解析器来为我插入语句的情况下实现?

Does anyone know if this behavior can be achieved without writing a parser to insert the statements in for me if it is the first package being declared inside file?

推荐答案

您可以构建自己的工具箱模块,该模块打开编译指示、加载模块和设置内容,然后直接加载.Import::Into 模块非常适合我们.

You can build your own toolbox module that turns on pragmas, loads modules and sets stuff, and just load that. The module Import::Into us great for that.

这是一篇博文,解释了如何做它.

但请注意,您设置的 Data::Dumper 的配置实际上与您设置它的包无关.这些是 Data::Dumper 包中的包变量,所以它们一旦设置就一直有效.在您的示例中,您基本上是用相同的东西再次覆盖它们.

But note that the config for Data::Dumper that you are setting is actually not related to the package you're setting it in. Those are package variables in the Data::Dumper package, so they are valid all the time once set. You're essentially overwriting them with the same stuff again in your example.

在生产代码中,除非有充分的理由,否则通常不应将多个包放在一个文件中.但这不会改变上面的任何建议.

In production code you should usually not put multiple packages in one file unless you have a good reason. But that doesn't change any of the advice above.

这篇关于Perl 导入到我所有的包中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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