What does “1;” mean in Perl?

烂漫一生 提交于 2019-11-28 20:54:35

问题


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;

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


回答1:


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 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;
}



回答2:


When you load a module "Foo" with use Foo or require(), perl executes the Foo.pm file like an ordinary script. It expects it to return a true value if the module was loaded correctly. The 1; does that. It could be 2; or "hey there"; just as well.

The block around the declaration of $somevar and the function Somesub limits the scope of the variable. That way, it is only accessible from Somesub and doesn't get cleared on each invocation of Somesub (which would be the case if it was declared inside the function body). This idiom has been superseded in recent versions of perl (5.10 and up) which have the state keyword.




回答3:


Modules have to return a true value. 1 is a true value.




回答4:


Perl modules must return something that evaluates to true. If they don't, Perl reports an error.

C:\temp>cat MyTest.pm
package MyTest;
use strict;
sub test { print "test\n"; }
#1;  # commented out to show error

C:\temp>perl -e "use MyTest"
MyTest.pm did not return a true value at -e line 1.
BEGIN failed--compilation aborted at -e line 1.

C:\temp>

Although it's customary to use "1;", anything that evaluates to true will work.

C:\temp>cat MyTest.pm
package MyTest;
use strict;
sub test { print "test\n"; }
"false";

C:\temp>perl -e "use MyTest"

C:\temp>  (no error here)

For obvious reasons another popular return value is 42.

There's a list of cool return values maintained at http://returnvalues.useperl.at/values.html.




回答5:


The curly braces limit the scope of the local variable $somevar:

{ my $somevar; ... } # $somevar's scope ends here




回答6:


From the documentation for require:

The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with 1; unless you're sure it'll return true otherwise. But it's better just to put the 1; , in case you add more statements.




回答7:


I don't know much about Perl, but usually you create a scope using curly braces. Probably $somevar shoudln't be available globally?



来源:https://stackoverflow.com/questions/1940182/what-does-1-mean-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!