quick scope question

1

1172948251

I want to maintain my $version variable in a single place
within the script and I want it available in a BEGIN block
that is a different package from the rest of the script. Is
this possible? Here's what I mean:

---------------

#!/usr/bin/perl

BEGIN {
package Some::package;
my $version = '1';
print "version is $version\n";
}

my $version = $Some::package::version;
print ("version is $version\n"); # not initialized ???
 
M

Mumia W.

I want to maintain my $version variable in a single place
within the script and I want it available in a BEGIN block
that is a different package from the rest of the script. Is
this possible? Here's what I mean:

---------------

#!/usr/bin/perl

BEGIN {
package Some::package;
my $version = '1';

Use "our" instead:

our $version = '1';
print "version is $version\n";
}

my $version = $Some::package::version;
print ("version is $version\n"); # not initialized ???

When you want a variable to be package scoped, use "our":

perldoc -f our
 
1

1172948251

Use "our" instead:

our $version = '1';



When you want a variable to be package scoped, use "our":

perldoc -f our

Using "our" works perfectly... Thanks!
 
T

Tad McClellan

I want to maintain my $version variable in a single place
within the script and I want it available in a BEGIN block
that is a different package from the rest of the script. Is
this possible? Here's what I mean:

---------------

#!/usr/bin/perl

BEGIN {
package Some::package;
my $version = '1';
^^
^^

See:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top