Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I find quite often that my old C habits are actually preventing me from using features in Perl.

For instance, I tend to initialize all variables when I declare them, because in C an uninitialized variable can, and usually does, contain garbage. However, this takes away the helpful "use of uninitialized variable" warning, so what worked for me in C really works *against* me in Perl. (Although the point about initializing to a default value is well taken)

Another habit I have is that of declaring all global variables at the top of the file. This can lead to having to go to the top of a file to find out about a variable that is used down in the body of the code somewhere. Then again, as has been pointed out already, that can be due to a poor variable name. On the other hand, if I don't have it at the top of the file, I have to go find it. I think part of this dilemma, for me, goes back to the following:

I still tend to use a lot of global variables, and I'm working at making variables just where I need them. (Keeps the "namespace" less cluttered? I'm still learning the lingo.) The classic example, for me, is the index variable of a for loop. No need, generally, to have it defined anywhere but within the loop, so why make it global? ("lexical" is the word I need?) And now that I've started learning about how to do OO programming in Perl, I can really start to "encapsulate" and avoid globals still more.

So I guess I'm just a recovering C addict. Thanks to the supportive Perl Monastery community, though, I'm beginning to kick the habit.

In reply to Re: To initialise or not to initialise? by melora
in thread To initialise or not to initialise? by kiat

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 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? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-10-27 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (95 votes). Check out past polls.

    Notices?