Why does Perl say Global symbol "SYMBOL" requires explicit package name at PROGRAM.pl line X?

I´m writing my first programs in Perl, and wrote this:

use strict;
use warnings;
$animal = "camel";
print($animal);

When I run it, I get these messages from the Windows command-line:

Global symbol "animal" requires explicit package name at stringanimal.pl line 3
Global symbol "animal" requires explicit package name at stringanimal.pl line 4

Please, could anyone what these messages mean?

Answers


use strict; forces you to declare your variables before using them. If you don't (as in your code sample), you'll get that error.

To declare your variable, change this line:

$animal = "camell";

To:

my $animal = "camell";

See "Declaring variables" for a more in-depth explanation, and also the Perldoc section for use strict.

P.S. Camel is spelt "camel" :-)

Edit: What the error message actually means is that Perl can't find a variable named $animal since it hasn't been declared, and assumes that it must be a variable defined in a package, but that you forgot to prefix it with the package name, e.g. $packageName::animal. Obviously, this isn't the case here, you simply hadn't declared $animal.


You have to put:

my $animal = "camel"

when using use strict.


Need Your Help

Testing web application on Mac/Safari when I don't own a Mac

macos safari

Having been caught out recently when a web site I launched displayed perfectly on IE, Firefox, Chrome and Safari on Windows but was corrupted when viewed using Safari on the Mac (by a potential cus...

Monotouch or Titanium for rapid application development on IPhone?

iphone xamarin.ios titanium appcelerator

As a .Net developer I always dreamed for the possibility to develop with my existing skills (c#) applications for the Iphone.