perl function our

In Perl, the our keyword is used to declare a package-global variable. It can be used inside a package or module to declare a variable that can be accessed from anywhere within the package or module.

Here's an example that demonstrates the usage of the our keyword:

refer to:‮gi‬iftidea.com
package MyPackage;

our $my_global_variable = "Hello, world!";

sub print_global_variable {
  print "The value of \$my_global_variable is: $my_global_variable\n";
}

1;

In this example, we define a package called MyPackage and declare a package-global variable $my_global_variable using the our keyword. We also define a subroutine called print_global_variable that prints out the value of the global variable.

Once the MyPackage module is imported or required, the $my_global_variable variable can be accessed from anywhere within the package or module. We can also access this variable from outside the package or module using the package name as a prefix, like this:

use MyPackage;

print "The value of \$MyPackage::my_global_variable is: $MyPackage::my_global_variable\n";

This will output:

The value of $my_global_variable is: Hello, world!
The value of $MyPackage::my_global_variable is: Hello, world!

Note that the our keyword does not create a new variable, but simply declares an existing one to be package-global. If the variable has not been declared before, it will be created as a package-global variable.