2

I have a program below, which I am trying to test:

#! 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

when I run it, I get messages:

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.

I don't understand, why Perl does not recognize global variables

2
  • 5
    I've seen a lot of comments on self-evident code, but # if statement has to be the best ever! Commented Oct 17, 2015 at 1:21
  • 2
    I think I saw "set x to 10" once. Commented Oct 17, 2015 at 12:20

1 Answer 1

6

$menu_sections->{$section} accesses an element of the hash referenced by the reference in scalar $menu_sections. You meant to access an element of the hash %menu_sections. You should have used $menu_sections{$section}.

The other error is the same.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.