requires explicit package name

S

sam

Hi,

I don;t know what is wrong with the following simple perl code:

#!/usr/bin/perl

use strict;
use warnings;

local %hash1 = ('sales_subtotal'=>100);
local %hash2 = ('sales_subtotal'=>150);


When I execute the above code, I got the following error:
Global symbol "%hash1" requires explicit package name at hashs.pl line 6.
Global symbol "%hash2" requires explicit package name at hashs.pl line 7.


Thanks
Sam
 
S

Sherm Pendley

sam said:
local %hash1 = ('sales_subtotal'=>100);
local %hash2 = ('sales_subtotal'=>150);

Have a look at "perldoc -f local", especially the first paragraph.

sherm--
 
S

sam

Bernard said:
Why do you think you need local() there?






Does this help (from perldoc perlsub)


==============
A `local' just gives temporary values to global (meaning package)
variables. It does *not* create a local variable. This is known as
dynamic scoping.
==============
Yup. thanks

?
 
M

Mothra

sam said:
use strict;
use warnings;

local %hash1 = ('sales_subtotal'=>100);
local %hash2 = ('sales_subtotal'=>150);


When I execute the above code, I got the following error:
Global symbol "%hash1" requires explicit package name at hashs.pl line 6.
Global symbol "%hash2" requires explicit package name at hashs.pl line 7.
use 'my' to scope a variable:

my %hash1 = ('sales_subtotal'=>100);
my %hash2 = ('sales_subtotal'=>150);
 
T

Tad McClellan

sam said:
I don;t know what is wrong with the following simple perl code:


See the documentation for the strict pragma. You are
running afoul of the "strict vars" part:

perldoc strict

use strict;


Here you promise to declare all of your variables, or to use the
full package name of the variable...

local %hash1 = ('sales_subtotal'=>100);


.... and here you use a variable doing neither.

You have broken your promise, perl refuses to run your program.


Why are you using package variables instead of lexical variables?

If you can use lexical variables, then you declare them like this:

my %hash1 = ('sales_subtotal'=>100);

If you really do need package variables (unlikely) then you declare
them like this:

our %hash1 = ('sales_subtotal'=>100);

or, you use its full name:

%main::hash1 = ('sales_subtotal'=>100);

When I execute the above code, I got the following error:
Global symbol "%hash1" requires explicit package name at hashs.pl line 6.


All of the messages that perl might issue are documented in

perldoc perldiag

You should look up messages there as a *first* step in troubleshooting:

=item Global symbol "%s" requires explicit package name

(F) You've said "use strict vars", which indicates that all variables
must either be lexically scoped (using "my"), declared beforehand using
"our", or explicitly qualified to say which package the global variable
is in (using "::").


If you don't know what this "package" vs. "lexical" thing is,
then please see:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top