Perl modules

Variable scope in module

my $var - is accessible only in module.
our $var - is accessible from outside.
$var - same as our $var.
use strict; - forces all variables to be qualified with "my" or "our".

Example of a variables in a module:
package Hello2;

use strict;

our $var1 = 1;
my  $var2 = 3;
$string = "Hello world!\n";

sub greet {
  return $string;

use Hello2;

print "var1 = $Hello2::var1 \n";

print "var2 = $Hello2::var2 \n";

print Hello2::greet();

Table of Contents.
Previous | Next.