When you declare a variable - you are not only declaring it's name (what follows the leading sigil), it's type (scalar, hash, array, typeglob, etc), you are also declaring it's scope. When you declare a variable with my, you are restricting its scope to the nearest enclosing curly braces.
Probably what you want to do is change its current value and not actually declare it again:
Conditionally declaring a variable is generally considered a bad idea and can lead to problems down the road. So your code would change to:my @array = qw(1 2 3); @array = qw(one two three) if $op eq 'alpha';
Finally, if you want to use the same variable name, but in different scopes - that is perfectly acceptable:my @xxx; @xxx = qw(one two three) if $op eq 'numbers'; @xxx = qw(four five) if $op eq 'alpha'; @xxx = qw(six ten) if $op eq 'alp';
I hope this helps - L~R#!/usr/bin/perl -w use strict; hello('world'); goodbye('world'); sub hello { my $greeting = shift; print "hello $greeting\n"; } sub goodbye { my $greeting = shift; print "goodbye $greeting\n"; }
In reply to Re: declaring same variable
by Limbic~Region
in thread declaring same variable
by samy1212
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |