"1"是什么意思?在Perl中意味着什么? [英] What does "1;" mean in Perl?

查看:120
本文介绍了"1"是什么意思?在Perl中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些Perl模块,例如,它们看起来类似于以下代码:

I have come across a few Perl modules that for example look similar to the following code:

package MyPackage;

use strict;
use warnings;
use constant PERL510  => ( $] >= 5.0100 );

require Exporter;

our @ISA = qw(Exporter);  
our @EXPORT = qw( );

{  #What is the significance of this curly brace?

    my $somevar;

    sub Somesub {
      #Some code here 
    }
}

1;

1;以及包围$somevar和Sub的花括号的意义是什么?

What is the significance of 1; and of the curly braces that enclose the $somevar and the Sub?

推荐答案

1表示该模块将true返回到use/require语句.它可以用来判断模块初始化是否成功.否则,use/require将失败.

1 at the end of a module means that the module returns true to use/require statements. It can be used to tell if module initialization is successful. Otherwise, use/require will fail.

$somevar是只能在块内部访问的变量.它用于模拟静态"变量.从Perl 5.10开始,可以使用关键字 state 关键字获得相同的结果:

$somevar is a variable which is accessable only inside the block. It is used to simulate "static" variables. Starting from Perl 5.10 you can use keyword state keyword to have the same results:

## Starting from Perl 5.10 you can specify "static" variables directly.
sub Somesub {
    state $somevar;
}

这篇关于"1"是什么意思?在Perl中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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