Learning Perl: 4.7. Notes on Lexical (my) Variables

本文探讨Perl编程中lexical变量(my)的概念,解释它们在子程序中的作用,如何避免全局变量污染,以及my变量在数组操作和错误处理中扮演的角色。
摘要由CSDN通过智能技术生成
Previous Page
Next Page

 

4.7. Notes on Lexical (my) Variables

Those lexical variables can be used in any block, not merely in a subroutine's block. For example, they can be used in the block of an if, while, or foreach:

    foreach (1..10) {
      my($square) = $_ * $_;  # private variable in this loop
      print "$_ squared is $square./n";
    }

The variable $square is private to the enclosing block; in this case, that's the block of the foreach loop. If there's no enclosing block, the variable is private to the entire source file. For now, your programs aren't going to use more than one source file, so this isn't an issue. But the important concept is that the scope of a lexical variable's name is limited to the smallest enclosing block or file. The only code that can say $square and mean that variable is the code inside that textual scope. This is a big win for maintainabilityif the wrong value is found in $square, the culprit will be found within a limited amount of source code. As experienced programmers have learned (often the hard way), limiting the scope of a variable to a page of code, or to a few lines of code, accelerates the development and testing cycle.

Also, the my operator doesn't change the context of an assignment:

    my($num) = @_;  # list context, same as ($num) = @_;
    my $num  = @_;  # scalar context, same as $num = @_;

In the first one, $num gets the first parameter, as a list-context assignment; in the second, it gets the number of parameters, in a scalar context. Either line of code could be what the programmer wanted; you can't tell from that one line alone, so Perl can't warn you if you use the wrong one. (Of course, you wouldn't have both of those lines in the same subroutine since you can't have two lexical variables with the same name declared in the same scope; this is just an example.) When reading code like this, you can always tell the context of the assignment by seeing what the context would be without the word my.

So long as we're discussing using my( ) with parentheses, remember that without the parentheses, my only declares a single lexical variable:[*]

[*] As usual, turning on warnings will generally report this abuse of my, or you can call 1-800-LEXICAL-ABUSE and report it yourself. Using the strict pragma, which we'll see in a moment, should forbid it outright.

    my $fred, $barney;                # WRONG! Fails to declare $barney
    my($fred, $barney);               # declares both

Of course, you can use my to create new, private arrays as well:[]

[] Or hashes, which you'll see in Chapter 6.

    my @phone_number;

Any new variable will start out empty: undef for scalars or the empty list for arrays.

Previous Page
Next Page
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值