Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I looked at the docs, looked at some threads, delved into the CB, and consulted the magic 8 ball, but I still can't seem to get my head around the answer to:

What is the difference between my, use vars, and our?

Most answers seemed vague, or I just didn't understand. The most worrysome answer was "Outlook not so good" from the magic 8-ball. I'm an NT admin. I know about Outlook not so good. Tell me about Perl scope, 8-ball. Perl scope!

What I think I know:

  • my declares variables in lexical scope. Yup, got it.
  • use vars allows you to declare global variables, and refer to $package::$foo as $foo. Right on.
  • and our is the same as use vars but with lexical scope. A globally lexically thingy. OK.

So on line 4 or 5, before any blocks, I declare my $foo. It acts with a lexical scope of the entire script. It acts sort of like a global variable.

What if I put our $foo in the same place. Do I get the same effect? What if I put use vars $foo?

My questions:

  1. Is there any difference between declaring variables at the beginning of scripts with my or our? Won't their scope be the same?
  2. Is there a reason to use one or the other in this situation

See this example. Look at %TOC. Should it be my or our?

Thanks for your help.

-OzzyOsbourne


In reply to Our, use vars, and magic, oh my! by OzzyOsbourne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-16 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found