When was the global (our) declaration introduced into perl?
One of my ISP's falls over when declaring as 'our' but is ok with 'my'.
Keith
One of my ISP's falls over when declaring as 'our' but is ok with 'my'.
Keith
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
"our" declarations
An "our" declaration introduces a value that can be best understood as
a lexically scoped symbolic alias to a global variable in the package
that was current where the variable was declared. This is mostly use-
ful as an alternative to the "vars" pragma, but also provides the
opportunity to introduce typing and other attributes for such vari-
ables. See "our" in perlfunc.
Our makes a variable available to the WHOLE script not just a sub or two (depending on place of declaration), I use OUR as follows...Is 'our' used to reference a var between 2 subs then?
################
# Start Module #
################
BEGIN {
# Invoke Exporter
use Exporter;
# Set Variables
our (@ISA, @EXPORT);
@ISA = qw(Exporter);
# Define global vars and subs to be exported
@EXPORT = qw( &getSQL &insSQL &updSQL &delSQL );
}
########################################################################
########################### GLOBAL SUBROUTINES #########################
########################################################################
########################
# INIT DSN VARIABLES #
########################
our $DSN;
##################
# Use SQL Module #
##################
use Sql;
# Set File DSN to use
$Sql::DSN = "full path/LiveSQL.dsn";
#!/usr/bin/perl -w
use strict;
# Script to show two functions sharing one scoped scalar.
set_value(12);
print get_value(), "\n";
set_value(63);
print get_value(), "\n";
# print $value; # Uncomment this line to see proof that $value is scoped
{
my $value;
sub set_value {
$value = shift;
}
sub get_value {
return $value;
}
}
No, you can use them (although possibly not wisely) if you fully qualify the name with the package.
For example you can use "$main::varname" everywhere, even under "use strict;".
If you declare a scalar with "my" you scope it and make it unavailable to anything outside that scope.
Fully qualified scalars are effectively globals (or more accurately globals within a package).