Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Scoping Rules For "our" Variables

by rehmanz (Novice)
on Jun 25, 2011 at 22:36 UTC ( [id://911413]=perlquestion: print w/replies, xml ) Need Help??

rehmanz has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to figure out the scoping rules for "our" variable types. I have the following two questions:
1) If I have decared a variable x in packages Foo, Bar and in the main program, what would be the output of the program below?
2) What would happen if I use the "use strict" option? Thanks in advance!

# Begin Package Foo package Foo; our $x = 1; # End Package
# Begin Package Bar package Bar; our $x = 2; # End Package
# Main Program use Foo; use Bar; our $x = 3; print "x without package reference = $x"; print "Foo x = $foo::x; # Output should be "Foo x = 1" print "Foo x = $bar::x; # Output should be "Bar x = 2" $x = 4; print "x without package reference = $x"; print "Foo x = $foo::x; # Output should be "Foo x = 1" print "Foo x = $bar::x; # Output should be "Bar x = 2"

Replies are listed 'Best First'.
Re: Scoping Rules For "our" Variables
by wind (Priest) on Jun 25, 2011 at 22:38 UTC

    Variable and package names are case sensitive. Simply replace that with $Foo::x and $Bar::x

    use strict and use warnings would've alerted you to the problem.

    package Foo; our $x = 1; package Bar; our $x = 2; package main; use strict; use warnings; our $x = 3; print "x = $x\n"; print "Foo::x = $foo::x\n"; print "Bar::x = $Bar::x\n"; $x = 4; print "x = $x\n"; print "Foo::x = $Foo::x\n"; print "Bar::x = $Bar::x\n"
Re: Scoping Rules For "our" Variables
by roboticus (Chancellor) on Jun 26, 2011 at 14:31 UTC

    rehmanz:

    I must admit that I'm somewhat perplexed by your question: You provide some code and then ask what it should produce by itself and with use strict. If you try running it, perl will show you what it *should* produce. (Unless you've found a bug in perl--but that's not going to be likely!)

    Your question about use strict shows that you're headed in the right direction, but if you try it with use warnings then perl will tell you why you're not getting the results you expect. In addition to the packages ones wind mentioned, you may find use diagnostics useful, too. It adds some information to the error messages that describe frequent causes of the errors and/or warnings. When I started learning perl, I used it frequently.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

    Update: I tweaked the first paragraph a tiny bit to make it flow better.

      Thank you! The use diagnostics command is extremely helpful.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://911413]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-05-25 18:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found