I am using Getopt::Long to parse command line flags and arguments for a perl script. For certain flags, I need to declare to variables within the script that are only used if that flag has been selected - otherwise they're never used. I want to declare them conditionally, i.e. only if the flag is used so that I don't have a bunch of unused variables sitting around. Here is some example code which doesn't work, but illustrates what I want to do.
use Getopt::Long qw(GetOptions);
my ($f1, $f2, $f3);
GetOptions('f1' => \$f1, 'f2' => \$f2, 'f3' => \$f3);
if($f2){
#declare some variables needed only if $f2 is turned on
}
Can anyone recommend the correct way to do this?