可以在子例程中使用模块吗? [英] Is it okay to use modules from within subroutines?

查看:76
本文介绍了可以在子例程中使用模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我开始使用OO Perl,并且我正在为我正在从事的新项目创建大量新对象.由于我不熟悉有关OO Perl的任何最佳实践,因此我们非常急于完成它:P

Recently I start playing with OO Perl and I've been creating quite a bunch of new objects for a new project that I'm working on. As I'm unfamilliar with any best practice regarding OO Perl and we're kind in a tight rush to get it done :P

我在每个函数中都添加了很多这类代码:

I'm putting a lot of this kind of code into each of my function:

sub funcx{
    use ObjectX; # i don't declare this on top of the pm file 
                 # but inside the function itself
    my $obj = new ObjectX;
}

我想知道这是否会对任何功能范围之外的Perl模块顶部的use Object行产生负面影响.

I was wondering if this will cause any negative impact versus putting on the use Object line on top of the Perl modules outside of any function scope.

我这样做是为了让我感觉很干净,以防需要转移功能.

I was doing this so that I feel it's cleaner in case I need to shift the function around.

我注意到的另一件事是,当我尝试在用于测试对象的unix服务器本身上运行test.pl脚本时,它的运行速度非常慢.但是,当通过连接到apache服务器的CGI运行相同的代码时,网页加载的速度就不会那么慢.

And the other thing that I have noticed is that when I try to run a test.pl script on the unix server itself which test my objects, it slow as heck. But when the same code are run through CGI which is connected to an apache server, the web page doesn't load as slowly.

推荐答案

在哪里使用?

use发生在编译时,所以放在哪里都没有关系.至少从纯粹务实的角度来看,它将起作用".因为它发生在编译时,所以即使将use置于条件条件中,也将始终执行它.永远不要这样做:if( $foo eq 'foo' ) { use SomeModule }

Where to put use?

use occurs at compile time, so it doesn't matter where you put it. At least from a purely pragmatic, 'will it work', point of view. Because it happens at compile time use will always be executed, even if you put it in a conditional. Never do this: if( $foo eq 'foo' ) { use SomeModule }

根据我的经验,最好将所有使用语句放在文件顶部.可以轻松查看正在加载的内容以及您的依赖项.

In my experience, it is best to put all your use statements at the top of the file. It makes it easy to see what is being loaded and what your dependencies are.

更新:

brian d foy指出了,在use语句之前编译的内容将不受此影响.因此,位置可能很重要.对于典型的模块,位置无关紧要,但是,如果它执行会影响编译的事情(例如,它导入具有原型的函数),则位置可能很重要.

As brian d foy points out, things compiled before the use statement will not be affected by it. So, the location can matter. For a typical module, location does not matter, however, if it does things that affect compilation (for example it imports functions that have prototypes), the location could matter.

此外, Chas Owens指出了会影响编译.设计用来更改编译的模块称为编译指示.按照惯例,语法以小写形式命名.这些效果仅在使用模块的范围内适用. Chas在其答案中以integer杂注为例.您还可以使用关键字no在有限范围内禁用编译指示或模块.

Also, Chas Owens points out that it can affect compilation. Modules that are designed to alter compilation are called pragmas. Pragmas are, by convention, given names in all lower-case. These effects apply only within the scope where the module is used. Chas uses the integer pragma as an example in his answer. You can also disable a pragma or module over a limited scope with the keyword no.

use strict;
use warnings;

my $foo;
print $foo;  # Generates a warning

{   no warnings 'unitialized`;  # turn off warnings for working with uninitialized values.

    print $foo;  # No warning here
}
print $foo; # Generates a warning


间接对象语法

在示例代码中,您具有my $obj = new ObjectX;.这称为间接对象语法,最好避免使用它,因为它可能导致模糊的错误.最好使用这种形式:


Indirect object syntax

In your example code you have my $obj = new ObjectX;. This is called indirect object syntax, and it is best avoided as it can lead to obscure bugs. It is better to use this form:

my $obj = ObjectX->new;

为什么测试脚本在服务器上运行缓慢?

没有办法告诉您所提供的信息.

Why is your test script slow on the server?

There is no way to tell with the info you have provided.

但找出答案的简单方法是为您的个人资料进行配置代码,并查看在哪里消耗了时间. NYTProf 是您可能想签出的另一种流行的分析工具.

But the easy way to find out is to profile your code and see where the time is being consumed. NYTProf is another popular profiling tool you may want to check out.

查看 Perl最佳做法,以及很好地总结了PBP的Damian Conway的OOP建议

Check out Perl Best Practices, and the quick reference card. This page has a nice run down of Damian Conway's OOP advice from PBP.

此外,您不妨考虑使用驼鹿.如果您可以接受较长的脚本启动时间,那么Moose是一个巨大的胜利.

Also, you may wish to consider using Moose. If the long script startup time is acceptable in your usage, then Moose is a huge win.

这篇关于可以在子例程中使用模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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