global variable scope in perl

E

Eswar

I need to move couple of functions from one perl file to another. In
this course the global variables that are used in the functions also
moved. I declared the global variables as 'our'. I am updating the
global variable in (1st)one function and using the same in
(Second)another function. when I print the variable in the secong
function, its printing null. But before the moment of the functions,
its printing the updated value int he 1st functions. I dont know why
its not printing after the movement. My friend suggested me to use
declare the variable as $::, then it looks fine. I wanted to know the
difference between our and $:: and why this dint work with our. If
anybody helps, I am very thankful.
 
D

Dr.Ruud

I need to move couple of functions from one perl file to another. In
this course the global variables that are used in the functions also
moved. I declared the global variables as 'our'. I am updating the
global variable in (1st)one function and using the same in
(Second)another function. when I print the variable in the secong
function, its printing null. But before the moment of the functions,
its printing the updated value int he 1st functions. I dont know why
its not printing after the movement. My friend suggested me to use
declare the variable as $::, then it looks fine. I wanted to know the
difference between our and $:: and why this dint work with our. If
anybody helps, I am very thankful.


See perldoc -f our.

Summary: "our" associates a simple name with a package variable in the
current package, for use within the current scope.
 
R

Rainer Weikusat

Eswar said:
I need to move couple of functions from one perl file to another. In
this course the global variables that are used in the functions also
moved. I declared the global variables as 'our'. I am updating the
global variable in (1st)one function and using the same in
(Second)another function. when I print the variable in the secong
function, its printing null. But before the moment of the functions,
its printing the updated value int he 1st functions. I dont know why
its not printing after the movement. My friend suggested me to use
declare the variable as $::, then it looks fine. I wanted to know the
difference between our and $:: and why this dint work with our.

The difference between our and $:: is that the former declares a
variable in the current package while the latter accesses a variable
in package main with a 'fully qualified path', cf

----------
our $a = 17;

package b;

our $a = 53;

print($a, "\t", $::a, "\n");
 
R

RedGrittyBrick

I need to move couple of functions from one perl file to another. In
this course the global variables that are used in the functions also
moved. I declared the global variables as 'our'. I am updating the
global variable in (1st)one function and using the same in
(Second)another function.

Whenever I find myself using a global variable I get an unpleasant
feeling and spend some time trying to think of a way of refactoring code
so I don't have to use global variables. Sometimes it is the expedient
thing to do but I usually prefer to switch to an OO approach and
instantiate an object to hold the variable and attach the
subroutines/functions as methods of that object class.
when I print the variable in the secong
function, its printing null. But before the moment of the functions,
its printing the updated value int he 1st functions. I dont know why
its not printing after the movement. My friend suggested me to use
declare the variable as $::, then it looks fine. I wanted to know the
difference between our and $:: and why this dint work with our. If
anybody helps, I am very thankful.

Here's a procedural way of sharing a "global" variable between two
functions. Of course, the variable is also visible to other unrelated
functions in the same file - and therefore vulnerable to unintended
alteration.

-----------------8<------------------ file x.pl
#!/usr/bin/perl
use strict;
use warnings;
use x;

set_foo(3);
print "foo is ", get_foo();

-----------------8<------------------ file x.pm
package x;
use Exporter;
@ISA = 'Exporter';
@EXPORT = qw(set_foo get_foo);

sub set_foo {
our $foo = shift;
}

sub get_foo {
return $foo;
}

1;
-----------------8<------------------

$ perl x.pl
foo is 3


You might want a `BEGIN { our $foo = -1; }` before the first sub in case
someone calls get_foo() without previously calling set_foo(). Probably
lots of other issues lurking. I'd use OO. Your Mileage May Vary.
Batteries not included.
 
E

Eswar

The difference between our and $:: is that the former declares a
variable in the current package while the latter accesses a variable
in package main with a 'fully qualified path', cf

----------
our $a = 17;

package b;

our $a = 53;

print($a, "\t", $::a, "\n");

Hi All,

Thank you very much for your help. I got the problem. I declared the
varibles as our and assigned to null at the top of the file. like

package A;
our $x = "";

The assignment ot null created the problem. because after I updated
the value in the Ist function and before accessing the variable in the
second function, there are many calls to the other fucntions with in
the package from different files. When others called some function in
the same package the variable are reinitialized to null again. I dint
observe this problem before the movement as there is no other
functions in previous package, so no calls. now I just declared the
variable as 'our $x;', solved my problem. I could have posted the code
in my query for better understanding of the problem.

Once again thankyou for ur help.
 
D

Dr.Ruud

The difference between our and $:: is that the former declares a
variable in the current package while the latter accesses a variable
in package main with a 'fully qualified path',

There is a high chance of failure when trying to reword documentation.
Why do you call it "the difference" where it is more probably "a
difference"?

----------
our $a = 17;

package b;

our $a = 53;

print($a, "\t", $::a, "\n");

For example realize that your second 'our $a' is in the same lexical scope.

perl -wle '
our $a = 1;

package b;

print "start b:", $a;
$a -= 5;

our $a = 12;
print "in b:", $a;

local $, = "\t";
print $a, $::a, $b::a, $main::a;
'
start b:1
in b:12
12 -4 12 -4
 
R

Rainer Weikusat

Dr.Ruud said:
There is a high chance of failure when trying to reword
documentation. Why do you call it "the difference" where it is more
probably "a difference"?

Because that's what the OP was asking for. I could also have replied
with "the spelling is obviously different, don't you think so?" or
"each character sequence occured in a different place of you text" or
with whatever other nonsense.
For example realize that your second 'our $a' is in the same lexical
scope.

I'm, 'for example', completely aware of that. I also have a brother
who has a boy child named Mortiz and I know a lot more absolutely
irrelevant information you'd hopefully not expect me to repeat here
each time.
 
R

Rainer Weikusat

Dr.Ruud said:
There is a high chance of failure when trying to reword
documentation. Why do you call it "the difference" where it is more
probably "a difference"?

Because that's what the OP was asking for. I could also have replied
with "the spelling is obviously different, don't you think so?" or
"each character sequence occured in a different place of you text" or
with whatever other nonsense.
For example realize that your second 'our $a' is in the same lexical
scope.

I'm, 'for example', completely aware of that. I also have a brother
who has a boy child named Moritz and I know a lot more absolutely
irrelevant information you'd hopefully not expect me to repeat here
each time.
 
R

Rainer Weikusat

Dr.Ruud said:

,----
| >> ----------
| >> our $a = 17;
| >>
| >> package b;
| >>
| >> our $a = 53;
| >>
| >> print($a, "\t", $::a, "\n");
| >> ----------
| >>
| >> As to "why doesn't work after the move" that's for some reason which
| >> is not obvious from the information you provided.
| >
| > For example realize that your second 'our $a' is in the same lexical
| > scope.
|
| I'm, 'for example', completely aware of that. I also have a brother
| who has a boy child named Moritz and I know a lot more absolutely
| irrelevant information you'd hopefully not expect me to repeat here
| each time.
`----

I think this should not only include the reaction ('*plonk*') but also
what caused it (stating that a particular assertion made by the 'Dr
Ruud' person was wrong and complaining about the fact that this
unwarranted assertion was made in the first place).
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top