为什么"Perl编程"对文件句柄使用本地(不是我的)? [英] Why does Programming Perl use local (not my) for filehandles?

查看:107
本文介绍了为什么"Perl编程"对文件句柄使用本地(不是我的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我阅读 Programming Perl ,第二版,第51页时,有些事情让我感到困惑:

When I read through Programming Perl, 2nd Edition, Page 51, something confuses me :

sub newopen {
    my $path = shift;
    local *FH;    #not my!
    open (FH, $path) || return undef;
    return *FH;
}

$fh = newopen('/etc/passwd');

我知道,为什么我们不建议您使用我的?到目前为止,如果使用my(),我看不到任何错误.

My I know, why we are not recommenced to use my? So far, I cannot see anything will go wrong if we use my().

谢谢!

推荐答案

最常见的答案是您必须使用local,因为my *FH是语法错误.

The trite answer is that you have to use local because my *FH is a syntax error.

正确"(但不是很有启发性)的答案是您做错了.您应该使用词汇文件句柄和open的三参数形式.

The "right" (but not very enlightening) answer is that you're doing it wrong. You should be using lexical filehandles and the three-argument form of open instead.

sub newopen {
    my $path = shift;
    my $fh;
    open($fh, '<', $path) or do {
        warn "Can't read file '$path' [$!]\n";
        return;
    }
    return $fh;
}

要真正回答为什么,需要解释词汇变量和全局变量之间以及变量范围和持续时间之间的区别.

To really answer why requires an explanation of the difference between lexical and global variables and between a variable's scope and its duration.

变量的范围是程序名称有效的部分.范围是静态属性.另一方面,变量的持续时间是动态属性.持续时间是程序执行期间变量存在并保存值的时间.

A variable's scope is the portion of the program where its name is valid. Scope is a static property. A variable's duration, on the other hand, is a dynamic property. Duration is the time during a program's execution that the variable exists and holds a value.

my声明一个词法变量.词法变量的范围从声明的角度到封闭的块(或文件)的末尾.您可以在不同的作用域中使用具有相同名称的其他变量而不会发生冲突. (您也可以在重叠的范围内重用名称,但是不要这样做.)词汇变量的持续时间是通过引用计数管理的.只要至少有一个对变量的引用,该值就存在,即使该名称在特定范围内无效! my也具有运行时效果-它使用给定名称分配 new 变量.

my declares a lexical variable. Lexical variables have a scope from the point of declaration to the end of the enclosing block (or file). You can have other variables with the same name in different scopes without conflict. (You can also re-use a name in overlapping scopes, but don't do that.) The duration of lexical variables is managed thorugh reference counting. So long as there is at least one reference to a variable the value exists, even if the name isn't valid within a particular scope! my also has a runtime effect -- it allocates a new variable with the given name.

local有点不同.它对全局变量起作用.全局变量具有全局范围(名称在任何地方都有效)和程序整个生命周期的持续时间. local的作用是临时更改全局变量的 value .有时将其称为动态作用域".更改从local声明的点开始,一直持续到封闭块的末尾,此后恢复了旧值.重要的是要注意,新值不限于块,它在任何地方都可见(包括子程序).引用计数规则仍然适用,因此您可以在更改过期后获取并保留对本地化值的引用.

local is a bit different. It operates on global variables. Global variables have a global scope (the name is valid everywhere) and a duration of the entire life of the program. What local does is make a temporary change to the value of a global variable. This is sometimes referred to as "dynamic scoping." The change starts at the point of the local declaration and persists until the end of the enclosing block after which the old value is restored. It's important to note that the new value is not restricted to the block -- it is visible everywhere (including called subroutines). Reference counting rules still apply, so you can take and keep a reference to a localized value after the change has expired.

回到示例:*FH是全局变量.更准确地说,它是"typeglob"-一组全局变量的容器. typeglob包含每个基本变量类型(标量,数组,哈希)的插槽以及其他一些内容.从历史上看,Perl使用typeglob来存储文件句柄,并对它们进行local大小化处理有助于确保它们不会相互干扰.词法变量没有typeglob,这就是为什么说my *FH是语法错误.

Back to the example: *FH is a global variable. More accurately it's a "typeglob" -- a container for a set of global variables. A typeglob contains a slot for each of the basic variable types (scalar, array, hash) plus a few other things. Historically, Perl used typeglobs for storing filehandles and local-izing them helped ensure that they didn't clobber each other. Lexical variables don't have typeglobs which is why saying my *FH is a syntax error.

在现代版本的Perl中,词汇变量可以并且应该用作文件句柄.这使我们回到了正确"的答案.

In modern versions of Perl lexical variables can and should be used as filehandles instead. And that brings us back to the "right" answer.

这篇关于为什么"Perl编程"对文件句柄使用本地(不是我的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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