Вопрос

I am new to perl, and have been trying to learn out of a book. I am using Komodo to run the scripts.

I am trying to create variables, but instead of coding what the book has put

$x=4;

I have to write

my $x=4;

Or else I get the following error

Global symbol "$x" requires explicit package name

Why is this? Can I change it at all?

Это было полезно?

Решение

You can get rid of that requirement by not using use strict;, I've no idea if you are seeing the error because you have use strict or because Komodo is adding it for you.

That said, however, do use use strict;. It catches all sorts of issues.

The book appears to be teaching you Perl 4 era coding practices. Perl 5 was released in 1994! Get a better book (such as Modern Perl or Learning Perl, 6th edition)!

Другие советы

when you use $x directly, you are using without use strict; I think this article will help you to understand the meaning of perl scope. http://perlmaven.com/scope-of-variables-in-perl

As others have pointed out, someone is adding use strict; to your code. This is a good thing and something that should always be done. It catches a lot of the errors that you may have in your code.

However, check your book and make sure it talks about the my and our commands. If it does, I'd go ahead and stick with the book. It's a beginner book, and is probably just getting you use to coding a few simple things without a lot of the magic incantations you use in Perl. The idea is to get you to write things like:

$x = 4;
print "Your number is $x\n";

And that's fine for now. Get you use to things. Give you confidence, draw you in, slowly, carefully, much like an angler fish draws in it's prey. It's okay... Perl is easy... Don't worry about a thing... There's nothing confusing about it... No need to run away before it's too late...

Then, SNAP! And, they'll be teaching you real Perl.


For now, if you don't want to put my everywhere, just so your coding examples match the book, put these two statements on the top of your program:

no strict;
no warnings;

This will shut down those two pragmas and let you get to following your book.

It's just like turning off the Emergency Backup System in a Nuclear Power plant. Now you can concentrate and not have to listen to all those annoying sirens going off all the time.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top