in reply to Syntax error - "my" within foreach loop

You already have good answers to your immediate question, but there are some slightly deeper issues to consider too. But first, it is great to see that you are using strictures (strict and warnings) in the first place - many Perl beginners don't.

Some languages (C and Pascal for example) require that all variables be declared in a block before any other statements. That often means the declaration is a long way from the first use and it can be hard to be sure where the first use is. Perl on the other hand allows you to declare variables pretty much anywhere you want to. That means that you can mostly declare a variable the first place it is used and assign a value to it as part of the declaration. This is a Good Thing&tm; because it makes it easier to see where and how the variable is used.

A more subtle thing is that inside a block (a loop body for example) you get a new "instance" (fresh sheet of paper to write the value on) every time the program flow enters the block and the variable in most cases can't be accessed outside the block.

There are many other things that you will learn about variables in Perl over time including closures and that the loop variable for a for loop is an alias and doesn't persist outside the loop. Enjoy your new adventure. ;)


Perl is environmentally friendly - it saves trees
  • Comment on Re: Syntax error - "my" within foreach loop

Replies are listed 'Best First'.
Re^2: Syntax error - "my" within foreach loop
by Anonymous Monk on Apr 14, 2008 at 16:24 UTC
    I like being able to declare variables anywhere, especially if they are unimportant accumulators or iterators. However I will often predeclare varibles early in the code or block where they are needed so that I can group them and describe them in a comment block. YMMV.

      A comment on a variable indicates in one place what the variable is for. An appropriately named variable tells the reader every place it's used what it is for.

      A comment that explains a variables role in following code should be placed with the code, not where a collection of variable declarations may huddle together at the top of a block or subroutine.

      A key element of good commenting is to place the comment as close to the real context for the comment as possible and to only to explain non-obvious elements of the code (there may be some disagreement over what is 'non-obvious').

      There is a lot of art in naming variables and writing good comments. There is much less art required in figuring out where to declare variables - code structure and the "late as possible" mantra dictates that.

      Something related to think about: a useful guide is that the length of an identifier should reflect the scope over which it is used - short identifiers for small scopes and longer identifiers for larger scopes. $tl is ok for a variable in a small scope, but $topLeft is better in a bigger scope, and $mainWndTopLeft may be appropriate in a large scope.


      Perl is environmentally friendly - it saves trees