Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > global variable scope in perl

Reply
Thread Tools

global variable scope in perl

 
 
Eswar
Guest
Posts: n/a
 
      07-20-2011
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.
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      07-20-2011
On 2011-07-20 11:21, Eswar wrote:

> 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.

--
Ruud
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-20-2011
Eswar <(E-Mail Removed)> writes:
> 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");
----------

As to "why doesn't work after the move" that's for some reason which
is not obvious from the information you provided.



 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      07-20-2011
On 20/07/2011 10:21, Eswar wrote:
> 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.


--
RGB
 
Reply With Quote
 
Eswar
Guest
Posts: n/a
 
      07-21-2011
On Jul 20, 5:08*pm, Rainer Weikusat <(E-Mail Removed)> wrote:
> Eswar <(E-Mail Removed)> writes:
> > 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");
> ----------
>
> As to "why doesn't work after the move" that's for some reason which
> is not obvious from the information you provided.


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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      07-21-2011
On 2011-07-20 14:08, Rainer Weikusat wrote:

> 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");
> ----------
>
> 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.

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

--
Ruud
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-21-2011
"Dr.Ruud" <(E-Mail Removed)> writes:
> On 2011-07-20 14:08, Rainer Weikusat wrote:
>
>> 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"?


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.

>> ----------
>> 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 Mortiz and I know a lot more absolutely
irrelevant information you'd hopefully not expect me to repeat here
each time.
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-21-2011
"Dr.Ruud" <(E-Mail Removed)> writes:
> On 2011-07-20 14:08, Rainer Weikusat wrote:
>
>> 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"?


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.

>> ----------
>> 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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      07-21-2011
On 2011-07-21 14:02, Rainer Weikusat wrote:

> I also have a brother
> who has a boy child named Mortiz and I know a lot more absolutely
> irrelevant information


*plonk*

--
Ruud
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-21-2011
"Dr.Ruud" <(E-Mail Removed)> writes:
> On 2011-07-21 14:02, Rainer Weikusat wrote:
>
>> I also have a brother
>> who has a boy child named Mortiz and I know a lot more absolutely
>> irrelevant information

>
> *plonk*


,----
| >> ----------
| >> 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Modifying a variable in a non-global outer scope? Edward C. Jones Python 1 05-19-2006 07:24 PM
global scope variable in .cpp file? shaun C++ 6 01-23-2006 04:37 PM
How to declare a variable in the global scope? Miguel Dias Moura ASP .Net 41 12-27-2004 05:56 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments