views:

519

answers:

7

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;

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

Many Thanks

+4  A: 

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

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

Pim
+8  A: 

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

David Dorward
+1  A: 

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

eWolf
+9  A: 

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.

tsee
+29  A: 

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

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

## starting from perl 5.10 you can specify "static" variables directly
sub Somesub {
    state $somevar;
}
Ivan Nevostruev
Upvote because this answer is both concise and correct and actually answers all of the parent's questions.
daxim
+6  A: 

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

Ed Guiness
+4  A: 

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.

toolic