global variable scope in perl

Discussion in 'Perl Misc' started by Eswar, Jul 20, 2011.

  1. Eswar

    Eswar Guest

    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.
     
    Eswar, Jul 20, 2011
    #1
    1. Advertising

  2. Eswar

    Dr.Ruud Guest

    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
     
    Dr.Ruud, Jul 20, 2011
    #2
    1. Advertising

  3. Eswar <> 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.
     
    Rainer Weikusat, Jul 20, 2011
    #3
  4. 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
     
    RedGrittyBrick, Jul 20, 2011
    #4
  5. Eswar

    Eswar Guest

    On Jul 20, 5:08 pm, Rainer Weikusat <> wrote:
    > Eswar <> 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.
     
    Eswar, Jul 21, 2011
    #5
  6. Eswar

    Dr.Ruud Guest

    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
     
    Dr.Ruud, Jul 21, 2011
    #6
  7. "Dr.Ruud" <> 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.
     
    Rainer Weikusat, Jul 21, 2011
    #7
  8. "Dr.Ruud" <> 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.
     
    Rainer Weikusat, Jul 21, 2011
    #8
  9. Eswar

    Dr.Ruud Guest

    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
     
    Dr.Ruud, Jul 21, 2011
    #9
  10. "Dr.Ruud" <> 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).
     
    Rainer Weikusat, Jul 21, 2011
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miguel Dias Moura

    How to declare a variable in the global scope?

    Miguel Dias Moura, Dec 14, 2004, in forum: ASP .Net
    Replies:
    41
    Views:
    10,858
    Vikesh Arora
    Dec 27, 2004
  2. Edward C. Jones
    Replies:
    1
    Views:
    450
    bruno at modulix
    May 19, 2006
  3. shaun
    Replies:
    6
    Views:
    815
    Alf P. Steinbach
    Jan 23, 2006
  4. David Filmer
    Replies:
    19
    Views:
    281
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    217
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page