For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > Perl error --> Global symbol requires...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Perl error --> Global symbol requires...
Bret Goodfellow

2005-02-25, 8:56 pm

I'm getting the following error in my code. If I define $i as my $i
then everything works fine. What's wrong?

# while1.pl
use strict ;
use warnings ;

$i = 1;

while ($i < 10) {
print "I am at $i\n";
i++;
}
# while1.pl



Global symbol "$i" requires explicit package name at
C:\BegPerl\while1.pl line 6

John W. Krahn

2005-02-25, 8:56 pm

Bret Goodfellow wrote:
> I'm getting the following error in my code. If I define $i as my $i
> then everything works fine. What's wrong?


perldoc -q "When I tried to run my script, I got this message. What does it mean"


> # while1.pl
> use strict ;
> use warnings ;
>
> $i = 1;
>
> while ($i < 10) {
> print "I am at $i\n";
> i++;
> }
> # while1.pl
>
> Global symbol "$i" requires explicit package name at
> C:\BegPerl\while1.pl line 6


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



John
--
use Perl;
program
fulfillment
Xiaofang Zhou

2005-02-26, 3:55 am

Hi, Bret

You must tell perl what kinds of var the $i is, when 'use=
strict'. $i can be
a lexical var or a global var.

my $i =3D 1;=09# $i is a lexical var.

our $i =3D 1; # $i is a global var, perl 5.6+ only.
use vars qw($i); # also as global var, but can work under old=
perl

=D4=DA 2005-02-25 15:55:00 =C4=FA=D0=B4=B5=C0=A3=BA
>I'm getting the following error in my code. If I define $i as=

my $i
>then everything works fine. What's wrong?
>
># while1.pl
>use strict ;
>use warnings ;
>
>$i =3D 1;
>
>while ($i < 10) {
> print "I am at $i\n";
> i++;
>}
># while1.pl
>
>
>
>Global symbol "$i" requires explicit package name at
>C:\BegPerl\while1.pl line 6


=D6=C2
=C0=F1=A3=A1

Xiaofang Zhou
xfzhou@xfzhou.homedns.org


Owen

2005-02-26, 3:55 am

On Fri, 25 Feb 2005 15:55:21 -0700
"Bret Goodfellow" <Bret.Goodfellow@questar.com> wrote:

> # while1.pl
> use strict ;
> use warnings ;
>
> $i = 1;
>
> while ($i < 10) {
> print "I am at $i\n";
> i++;
> }
> # while1.pl
>
>
>
> Global symbol "$i" requires explicit package name at



Try removing the 'use strict;' statement. See what happens

You also have another problem with that script, perhaps i++ was a typo?



Owen
John W. Krahn

2005-02-26, 3:55 am

Owen wrote:
> On Fri, 25 Feb 2005 15:55:21 -0700
> "Bret Goodfellow" <Bret.Goodfellow@questar.com> wrote:
>
>
> Try removing the 'use strict;' statement. See what happens


That is like saying that you can "fix" the grinding noise in your car's engine
by turning up the stereo so that you can't hear it. :-)


John
--
use Perl;
program
fulfillment
JupiterHost.Net

2005-02-26, 3:55 am

Owen wrote:
> On Fri, 25 Feb 2005 15:55:21 -0700
> "Bret Goodfellow" <Bret.Goodfellow@questar.com> wrote:
>
>
> Try removing the 'use strict;' statement. See what happens


Better put on your fire resistant flame suit, thats some pretty bad
advice :)

> You also have another problem with that script, perhaps i++ was a typo?


Exactly why you don't remove strict but fix the problem :)

#!/usr/bin/perl

use strict;
use warnings;

my $i = 1; # problem one: use my()
while($i < 10) {
print "I am $i hear me roar\n";
$i++; # problem 2 $i not i
}

# or even sexxier :)

print "I am $_ hear me roar\n" for(1..9);
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com