Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Help needed understanding global variables in Perl

by Sinister (Friar)
on Mar 05, 2002 at 09:12 UTC ( #149306=note: print w/ replies, xml ) Need Help??


in reply to Help needed understanding global variables in Perl

Mmmm...
First of all: Global variables are not the best thing man has ever invented. It is better to have your routines talk to each other by what they return, then trough the use of global variables. (loose coupling)

But, sometimes, we do need globals. In that case, if you have read the perldoc on our (do `perldoc -f our` on your $SHELL) you'll notice, that with our you can create a variable that goes beyond even package scope. In that case you would never want to use our inside a routine, because you would like the routine data to be private to that routine.
eg:

#!/perl -wT use strict; sub foo { my $foo = "hello"; } sub bar { our $bar = "world"; } foo(); bar(); print "$foo $bar\n";

Would actualy print " world", since $foo is routine scope and $bar is lexical scope.
Defining a global variable would require you to do: my $foo in the main scope of a 'package'.
eg:
#!/perl -wT use strict; my $foo = "" our $bar = "" sub foo { $foo = "hello"; } sub bar { $bar = "world"; } foo(); bar(); print "$foo $bar\n";

This will actualy print "hello world" - which would have worked perfectly if you had choosen to do my $bar = ""; instead of our.

Now to get back on the difference between our and use vars

NOTE: The functionality provided by this pragma has been
superseded by "our" declarations, available in Perl v5.6.0
or later. See the our entry in the perlfunc manpage.


These lines come directly from the documentation of vars itself.

I hope this has answered some of your questions.

A righthanded-lefthand....
"Field experience is something you don't get until just after you need it."


Comment on Re: Help needed understanding global variables in Perl
Select or Download Code
Re: Re: Help needed understanding global variables in Perl
by gellyfish (Monsignor) on Mar 05, 2002 at 10:25 UTC

    A significant difference between use vars and our is that use vars is global in its effect whereas the effect of our is limited to the same lexical scope as my.

    /J\

Re: Re: Help needed understanding global variables in Perl
by rbrito (Acolyte) on Mar 06, 2002 at 02:42 UTC

    Your first program doesn't work with warnings and strict turned on and I guess that this was what I asked in the original post (I was the original poster): the interpolation of $foo in your print function is illegal, since foo is only valid within the sub foo.

    Also, you say that:

    Defining a global variable would require you to do: my $foo in the main scope of a 'package'.
    I guess that you meant declaring a global variable, right?

    It seems that using my $foo in package main doesn't seem to interfere with $main::foo anyway as can be seen in the following code snippet:

    #!/usr/bin/perl use strict; use warnings; use diagnostics; package main; my $foo = "hello"; $main::foo = "world"; print "$foo $main::foo\n";
    (Isn't it weird? It is terrible to think one knows the language after reading a lot about it, but still fail to understand things which should be simple semantics of the language. I would guess that the my $foo under the package main would manipulate the symbol table such that $foo were equal to $main::foo).

    If $foo and $main::foo were the same variable, then the program would print world world, which it does not.

    Furthermore, if you fully qualify your variables in package main, then you don't need to worry with declaring them with my, nor with our, nor with use vars, as can also be seen in the program above (see that $main::foo is not declared; yet, the compiler doesn't complain about it, even though the program is running under use strict).

      It seems you are completely correct. I was under the distinct assumption that defining a variable in a scope (aka declaring) trough the use of our, lifted that variable out of the scope and accesible for other scopes. This seems only trough when using no strict 'vars' (but that would not be nice).

      <off-topic>
        Does it matter whetter we call 'this' defining or declaring? I know the difference between the two, and if you are a nice perl -wT && strict programmer you define and declare all at once, to prevent warnings.
      </off-topic>

      Your snippet leads to the suspicion that more evil-hack's are possible with perl, then I so far realized.
      I off course tested your snippet, and it works prima!
      However if you would add these lines:
      package not_main; { print $foo; print $main::foo; print "\n"; } package main;
      It would _still_ work fine. This puzzles me. (Why one would define more packages inside one script puzzles me as well)

      As you I was somewhat confinced that I knew the language and the behavior of the interpreter by now. This has once again proofed me wrong, and that is what keeps perl interesting!

      And then, last but not least: thnx 4 ur reply ;-)

      A righthanded-lefthand....
      "Field experience is something you don't get until just after you need it."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://149306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-09-17 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls