Operating System - HP-UX
1821983 Members
3364 Online
109638 Solutions
New Discussion юеВ

Error in perl: Global symbol requires explicit package name

 
SOLVED
Go to solution
Tapas Jha
Valued Contributor

Error in perl: Global symbol requires explicit package name

Hi,
I am trying to learn perl and before that I have installed perl5.8 in hp-ux 11.0 OS. Here is the
swlist output: perl D.5.8.0.B Perl Programming Language

After perl installation everything is /opt/perl/ directory. Below are the bin,lib,man,html dir.
/opt/perl/bin ;/opt/perl/html;/opt/perl/lib ; /opt/perl/man

Earlier i have perl 4 in my system.I have moved earler perl found in /usr/local/bin and
/usr/contrib/bin to perl4.0.
Now to compile perl program and to get all features of perl program what sort of changes or
configuration do i need to perform?

I am getting below error while compiling one perl program.
""""
Global symbol "%words" requires explicit package name at qsn_ansugng_qw.pl line 6
Global symbol "$name" requires explicit package name at qsn_ansugng_qw.pl line 13
syntax error at qsn_ansugng_qw.pl line 22, near "$words("
Global symbol "$guess" requires explicit package name at qsn_ansugng_qw.pl line 26
Global symbol "$secretword" requires explicit package name at qsn_ansugng_qw.pl line 26
... etc
"""""

Below is my program.
#!/opt/perl/bin/perl -w
use strict;
%words = qw(
fred camel
barney llama
betty alpaca
wilma alpaca
);
print "What is your Name?" ;
$name = ;
chop ($name);
if ( $name eq "Tapas" )
{
print "Hello, $name! How good of you to be here!\n";
}
else
{
print "Hello, $name!\n";
$secretword = $words($name);
print "What is the secret word !";
$guess=;
chop ($guess);
print "Invalid Guess!! Try Again !!!\n";
print "What is the secret word !";
$guess=;
chop ($guess);
}
}

Will appreciate if get proper guidance not only for this but also how to learn perl properly.(Full online help)

Thanx in advance.

Rgds
Tapas
Tapas Jha
3 REPLIES 3
H.Merijn Brand (procura
Honored Contributor

Re: Error in perl: Global symbol requires explicit package name

The errors are due to your "use strict;" statements which imposes stricter behaviour on the perl parser.

In perl-4, one has global symbols, and one could give it a temporary value with 'local', which would restore the original value on end of scope.

In perl-5, these type of values still exist, but now that we have modules, we don't want the variables to `leak' to outside a script. Because variables are refered to by name - though namespace hashes - they will not only be visible to the cruel outside world, but they will also be modifiable (changeable, alterable).

In perl-5 we now have lexical variables, only known to the scope in which they are declared, and these do not live in a namespace hash, but in something called a pad.

Lexical vaiables are declared with the keyword 'my':

my $foo = 1;
my @bar = (1, "test", 3.5, sqrt 4);
my %baz = ( green => '#00ff00', red => '#ff0000');

if no scope is active (a block, or a package), the default scope for a variable is the file (script).

--8<---
#!/opt/perl/bin/perl
use strict;
use warnings; # replaces -w, but much better

my %words = (
fred => "camel",
barney => "llama",
betty => "alpaca",
wilma => "alpaca",
);
print "What is your Name?";
chomp (my $name = ); # chomp is a safe chop.
if ($name eq "Tapas") {
print "Hello, $name! How good of you to be here!\n";
}
else {
print "Hello, $name!\n";
my $secretword = $words{$name}; # hashes need braces, no parens
print "What is the secret word !";
chomp ($guess = );
if ($guess ne $secretword) {
print "Invalid Guess!! Try Again !!!\n";
print "What is the secret word !";
chomp ($guess = );
}
}
}
-->8---

# perldoc strict
# perldoc -f chomp
# man perlvar

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Tapas Jha
Valued Contributor

Re: Error in perl: Global symbol requires explicit package name


Thanx Procura. Great!!

Now It's working but not accurately. During compling It was giving error "global symbol requires" and i have changed three lines where chomp was there. Put my eg; chomp (my $guess=);

But this program is not matching name, secretword sequesnce. Instead of if condition if you put while then it should terminate when matches but it's not.

Rgds
Tapas
Tapas Jha
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Error in perl: Global symbol requires explicit package name

Look at the lines where I inserted 'my' in front of the variable.

To loop till a valid answer is given:

my $secretword = $words{$name};
for (;;) {
print "Hello, $name!\n";
print "What is the secret word !";
chomp ($guess = );
$guess eq $secretword and last;
print "Invalid Guess!! Try Again !!!\n";
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn