in reply to Re^2: Help! My variables are jumping off a cliff!
in thread Help! My variables are jumping off a cliff!
Is there ever a situation in which declaring a variable twice in the same scope is useful? Conversely, is there ever a time when doing so will not cause problems?
Yes - when a variable is no longer needed, then there's no harm in redeclaring it and using it for a different purpose.
For example, imagine the following test script:
use Test::More; use strict; no warnings; diag "Testing that 'uc' works."; plan tests => 3; my $result = uc 'foo'; is $result, 'FOO'; my $result = uc 'bar'; is $result, 'BAR'; my $result = uc 'baz'; is $result, 'BAZ';
Certainly different variables could be used for each test, or the my could be dropped from the second and third tests, but why should Perl insist upon it. The code above is perfectly clear, and runs fine.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Help! My variables are jumping off a cliff!
by oko1 (Deacon) on Feb 26, 2012 at 15:02 UTC | |
by JavaFan (Canon) on Feb 26, 2012 at 22:29 UTC | |
by oko1 (Deacon) on Feb 27, 2012 at 00:50 UTC | |
by Your Mother (Archbishop) on Feb 27, 2012 at 01:22 UTC | |
by JavaFan (Canon) on Feb 27, 2012 at 08:45 UTC | |
by tobyink (Canon) on Feb 26, 2012 at 16:07 UTC |