Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you have to put a 1; at the end of a Perl 5 module?

Tags:

module

perl

Why do all Perl 5 modules have to end with 1;?

like image 506
raza Avatar asked Aug 31 '10 06:08

raza


People also ask

What does 1 mean in perl?

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.

How do Perl modules work?

There are two main ways of loading a module in Perl. You can do it at compile time and at run time. The perl1 compiler (yes, there is a compiler although it's interpreted language) loads files, compiles them, then switches to run time to run the compiled code.

How many Perl modules are there?

Perl modules are a set of related functions in a library file. They are specifically designed to be reusable by other modules or programs. There are 108,000 modules ready for you to use on the Comprehensive Perl Archive Network.


2 Answers

A Perl module must end with a true value or else it is considered not to have loaded. By convention this value is usually 1 though it can be any true value. A module can end with false to indicate failure but this is rarely used and it would instead die() (exit with an error).

source: wiki

like image 105
codaddict Avatar answered Dec 02 '22 00:12

codaddict


That's because use Module LIST; is equivalent to BEGIN { require Module; Module->import( LIST ); } (see perldoc -f use)

Particularly, require Module; can be implemented with the following bit of code (from perldoc -f require):

sub require {
  my ($filename) = @_;
  if (exists $INC{$filename}) {
    return 1 if $INC{$filename};
    die "Compilation failed in require";
  }
  my ($realfilename,$result);
  ITER: {
    foreach $prefix (@INC) {
      $realfilename = "$prefix/$filename";
      if (-f $realfilename) {
        $INC{$filename} = $realfilename;
        $result = do $realfilename; ## HERE
        last ITER;
      }
    }
    die "Can't find $filename in \@INC";
  }
  if ($@) {
    $INC{$filename} = undef;
    die $@;
  } elsif (!$result) { ## AND HERE
    delete $INC{$filename};
    die "$filename did not return true value";
  } else {
    return $result;
  }
}

See that do $realfilename? Look at perldoc -f do, and you'll see that it's the equivalent, pretty much, of eval system("cat $realfilename"), which basically means slurp the file in, and eval it.

Looking at perldoc -f eval yields: "In both forms, the value returned is the value of the last expression evaluated inside the mini-program". That is, the last statement in the file.

It's worth to mention that if the last "statement" is a subroutine declaration, that does not constitute a true value:

$ perl -le'$r = eval "sub a {1}"; print $r ? "true" : "false"'
false

Whereas a 1; after it does:

$ perl -le'$r = eval "sub a {1} 1"; print $r ? "true" : "false"'
true

Looking again at the sub require above, and with the knowledge of what require and do does, the $result that the do $realfilename returns is indeed the last statement evaled from the file, which is indeed the last statement in the file.

If such last statement isn't a true value, the ## AND HERE elsif() I highlighted would be called, and the require would die as the module does not return a true value. The use Module would also die, as it's doing a require behind the scenes.

Long story short, it doesn't matter what you end the file with, as long as it's a value that is understood as being true by perl. Samples on the wild (CPAN) include 1;, although some these days rather end their files in 1 (no missing semicolon), and others just weirdly use stuff like '0 but true'; or 'supercalifragilisticexpiralidous';, all of which are parsed as "truish" values by Perl.

Hope the above is to your satisfaction ;)

Happy hacking!

like image 28
mfontani Avatar answered Dec 02 '22 01:12

mfontani