脚本无法识别全局变量

时间:2015-10-16 17:54:53

标签: perl

我有一个程序,我正在尝试测试:

#! usr/bin/perl    
#MOA is our company module    
#the perl is running for windows 7    

use strict;    
use warnings;    

use Getopt::Long;    
use MOA::CLSUtils;    
use MOA::PamReport;    
use MOA::PamJobs;    
use Data::Dumper;    
use Time::HiRes qw(gettimeofday);    
use Date::Pcalc;    
use File::Path  qw(make_path remove_tree);    

#declare global varibles 

my (%menu_sections, %menu_section, %menu_items, %menu_index);  

#initialize global variables
%menu_sections = (

#initialize hash
     );    
%menu_items =(#initialize hash);    
my $TERM_WIDTH = 44;    #declare variables and initialize
my ($section, $item, $knote);    
my $count = 1;    
my $submenu="N";    

#foreach loop compare keys
#the code below is trying to print menu for users
foreach  $section (sort {$a cmp $b} keys %{menu_sections}) {    
if (($count == 1) && ($submenu eq "N")) {    #if statement
}    
else {    #else statement
    $knote = "";    
}    
    #print on the screen using category function
print "\n\t", category($section, $knote), "\n", "\t", "-" x length($section), "\n";   
    #the second key print using category function 
foreach $item (@{$menu_sections->{$section}}) {    #line 36 go through  hash and code don't recognize hash

    printf("\t%3s )   %-15s  %-30s\n", $item, $menu_items->{$item}->[0], $menu_items->{$item}->[1]);    #line 38 print menu
}    
$count++;    #count lines
}    #end of the code

当我运行它时,我收到消息:

Global symbol "$menu_sections" requires explicit package name at test.pl line 36
Global symbol "$menu_items" requires explicit package name at test.pl line 38    
Global symbol "$menu_items" requires explicit package name at test.pl line 38
Execution of test.pl aborted due to compilation errors.

我不明白,为什么Perl不识别全局变量

1 个答案:

答案 0 :(得分:6)

$menu_sections->{$section}访问标量$menu_sections中引用引用的哈希元素。您打算访问哈希%menu_sections的元素。您应该使用$menu_sections{$section}

另一个错误是相同的。