Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

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

Hi. I'm a newbie Perl programmer (but a relatively experienced programmer in C) and I'm having trouble understanding the use of global variables in Perl. I would appreciate if people could help me.

The problem that I'm having is actually very simple: how exactly do I define (rather than declare) a global variable in Perl? (I know that declaring the variable may be done with use vars.)

I see that even under use strict, I don't have to declare/define global variables in Perl: I just need to fully qualify them and they're already there. Is this correct? If it is, then is there any kind of "super" use strict, which helps me catch typos that my programs may have?

Also, is there any way of defining a variable like C's static (i.e., a global variable with local scope). I see that Perl's our is almost what I want, but the variable declared with our in a function is still accessible outside the function if I use the variable's fully qualified name. I fear that I don't completely understand our.

In fact, I have trouble understanding the difference between our ($i); and use vars qw($i); Can anybody help with the difference? They don't seem different to the eyes of this poor Perl newbie.

Thanks in advance for any comments.


In reply to Help needed understanding global variables in Perl by Anonymous Monk

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 chilling in the Monastery: (5)
As of 2024-04-18 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found