38

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anand Shah
  • 14,575
  • 16
  • 72
  • 110
  • 2
    Also, [Why do you have to put a 1 at the end of a perl5 module?](http://stackoverflow.com/questions/3606528/why-do-you-have-to-put-a-1-at-the-end-of-a-perl-5-module) and [Cool return values](http://returnvalues.useperl.at/values.html). – Lazer Oct 07 '10 at 06:29
  • I'm disappointed that tchrist didn't submit an answer for this. – Karl Knechtel Jun 09 '11 at 03:02
  • Technically, this is two questions in one. I normally try to nip these in the bud and edit one question out before answers come in, but it's too late now. – Flimm Mar 11 '15 at 13:02

7 Answers7

69

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82
  • 6
    Upvote because this answer is both concise and correct and actually answers all of the parent's questions. – daxim Dec 21 '09 at 14:21
11

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
  • 5,034
  • 1
  • 19
  • 27
9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
8

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

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

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

Pim
  • 1,049
  • 6
  • 4
4

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
  • 57,801
  • 17
  • 75
  • 117
1

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

eflorico
  • 3,589
  • 2
  • 30
  • 41