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:

Hello2.pmprogram2.pl
package Hello2;

use strict;

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

sub greet {
  return $string;
}
1;
#!/usr/bin/perl

use Hello2;

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

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

print Hello2::greet();


Table of Contents.
Previous | Next.