On Feb 28, 2013, at 10:31 AM, Chris Stinemetz wrote:

> I want to put a hash declaration in a separate file from the main script.
> How do I do this correctly?
> 
> perl.pl ( main script )
> 
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Data::Dumper;
> 
> require "lib.pl";
> 
> print Dumper \%hash;
> 
> lib.pl ( library script )
> 
> #!/usr/bin/perl
> use warnings;
> use strict;
> 
> my %hash = (
>   "Version" => 0,
>   "SRT" => 11,
>   "SRFC" => 12,
>   "CFC" => 21,
>   "CFCQ" => 22,
>   "ICell" => 29,
>   "ISector" => 30,
>   "Cell" => 31,
>   "Sector" => 32,
> );
> 
> 1;
> 
> The error I am getting:
> 
> Global symbol "%hash" requires explicit package name at perl.pl line 8.
> Execution of perl.pl aborted due to compilation errors.

Put the following line in the main script:

our %hash;

Change the "my %hash = ( ... );" declaration in lib.pl to "our %hash = (...);".

In your original versions, %hash was a lexical variable in lib.pl, and thus not 
in scope within perl.pl. Using 'our' instead of 'my' makes %hash a package 
variable (in package main::), and as such is accessible by both perl.pl and 
lib.pl, The 'our' declaration also lets you leave off the package name when you 
access the variable.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to