5

I am trying to figure out how I can use the same variables in my if/else blocks. For example,

$var1
$var2
$var3

if(condition) {
...
}
else {
...
}

I need to access $vars1, 2, and 3 in the blocks, but I cannot. I think I have a scoping issue. I tried making them global, but I see many errors and the program doesn't run properly. What am I missing?

Here is my code. My issue is that the variables at the bottom(@varList, etc) are needed for the server because this is a script on an apache server that also uses CGI. it spits out errors like this:

[error] Global symbol "$questionslist" requires explicit package name at /home/megaoff/www/vi
ewquestions.dhtml line 46.\nGlobal symbol "$site" requires explicit package name at /home/megaoff/www/viewquestions.dhtm
l line 46.\nGlobal symbol "$xs" requires explicit package name at /home/megaoff/www/viewquestions.dhtml line 46.\nGlobal
 symbol "$username" requires explicit package name at /home/megaoff/www/viewquestions.dhtml line 46.\n

#!/usr/bin/perl

use strict;
use CGI;
use BarryP;

my $pagev = BarryP::makeP("noextracook", 1);
my $bvga = $pagev->{'vga'};
my %vga = %$bvga;
my $cgi = CGI->new;
my $usePage = "answerquestions.html";
my $anslist = "/home/megaoff/www/limages/anslist.txt";
my $unanslist = "/home/megaoff/www/limages/unanslist.txt";

my $action = $vga{"action"};


if($action eq 'adminmode') {
    my @a_list = $pagev->listFile($anslist);
    my %list = map { split(/\t/, $_, 2) } @a_list;
}   
else {
    my @u_list = $pagev->listFile($unanslist);
    chomp @u_list;
    my %questions = map { $_ => '' } @u_list;
    my $question = $cgi->param('question');
    my $answer = $cgi->param('answer');
    chomp($question, $answer);

    open(my $ANS, '>>', $anslist) or die "Can't open file $anslist: $!";
    print $ANS "$question\t$answer\n";
    close($ANS) or die "Can't close file $anslist: $!";

    delete $questions{$question};
    open(my $UNANS, '>', $unanslist) or die "Can't open file $unanslist: $!";
    print $UNANS "$_\n" foreach keys %questions;
    close($UNANS) or die "Can't close file $unanslist: $!";

    my $questionslist = join("<br>", @u_list);
    my $site = $pagev->{'site'};
    my $xs = $pagev->{'xs'};
    my $username = $pagev->{'username'};
}

my @varList = ('questionslist', $questionslist, 'action', $action, 'site', $site, 'xs', $xs, '$username', $username);

$pagev->pageHeader($usePage, @varList);
4

2 回答 2

6

你的问题是范围界定。在第 46 行,当您引用 $questionlist 等时,它们已经超出范围,因为您在 else 块中定义了它们。

你有(简化):

if ($blah) {
  # do stuff
} else {
  # do other stuff
  my $variable = "something";
}

doSomethingWith($variable); # illegal, $variable is not in scope.

$variable没有在 else 块之外定义,并且 else 块甚至可能不会被执行。您要么需要将函数放在 else 块中,要么找到某种方法为这些变量提供默认值,并在 if 语句之前声明它们。

于 2013-07-03T20:51:50.847 回答
1

理想情况下,您的变量声明应该具有尽可能紧凑的范围,同时仍处于需要访问变量的代码范围内。

my $var1 = 'aBcD';
my $var2;
if ($uc) {
   $var2 = uc($var1);
}
else {
   $var2 = lc($var1);
}

当然可以这样写

my $var1 = 'aBcD';
my $var2 = $uc ? uc($var1) : lc($var1);
于 2013-07-03T20:31:33.207 回答