in reply to where to declare a variable...
Dvergin is indeed correct when he says that you should declare @entries right before the if blocks; assuming, that is, that you are going to be using it outside of the if blocks. However, if the usage is going to be exclusive to inside of the blocks, you may want to consider declaring it twice, once for each block. Garbage collection is done at the end of the block the variable is declared in, so you will want your variables garbage collected as soon as possible if you are no longer using it. Here is an example:
my $entry = 1; my $item; while($entry) { $item = "foobar"; print $item; $entry--; }
In this example, $item is declared out of the loop, even though it is not used outside of the loop. Since it wasn't going to used outside of the loop, it should have been declared inside of the loop so that it was destroyed (and the memory it uses freed) when the block exits.
From looking at the context of your code, it appears that you might want to use @entries outside of the if statements, so you should follow dvergin's suggestion. Otherwise, always make sure that you declare your variables in the inner-most possible block that you can so that you don't unnecessarily tie up memory
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re(2): where to declare a variable...
by dmmiller2k (Chaplain) on Dec 09, 2001 at 23:21 UTC | |
by Ven'Tatsu (Deacon) on Dec 10, 2001 at 00:54 UTC |