1
votes

I am new to Perl and I have been following a book. All is well, except for whenever I try to initialize a variable as shown by the book, I am getting an error like below. Kindly tell me what should I do in order to avoid this error?

Code:

#!/usr/bin/perl -w
use 5.014;
use strict;
use utf8;

$radius = <STDIN>;
$circum;
if ($radius<0){
$circum = 0
} else{
    $circum = 2*3.141*$radius;
}

print $circum."\n";

Errors: Global symbol "$radius" requires explicit package name at ./example1 line 6. Global symbol "$circum" requires explicit package name at ./example1 line 7. Global symbol "$radius" requires explicit package name at ./example1 line 8. Global symbol "$circum" requires explicit package name at ./example1 line 9. Global symbol "$circum" requires explicit package name at ./example1 line 11. Global symbol "$radius" requires explicit package name at ./example1 line 11. Global symbol "$circum" requires explicit package name at ./example1 line 14. Execution of ./example1 aborted due to compilation errors.

Also, I have read somewhere in the forums about 'our' and 'my' keywords. Using these seems to work. But, is it compulsory to use these keywords. If so, I think it is strange that the book did not include them.

2
Take out use strict; and it won't be compulsory. - Elliott Frisch
If you use strict then 'my' or 'our' is compulsary - abasterfield
Yet use strict; and use warnings; should start every Perl script so indeed it is strange that your book did not tell you about this stuff. - DeVadder
use 5.014 automatically loads strict (and warnings) - mob

2 Answers

2
votes

The issue is the 'use strict' (which is actually a very good thing).

The result is that your variables need to be declared as follows:

my $radius = <STDIN>;

The 'my' keyword, there, makes the variable local to the current scope. And the 'use strict;' says you must declare all variables and specify their scope. The two typical ways you'd want to declare variables are:

my  $localOnly;
our $shareableVariable;

Basically: use 'my' when you don't want anyone else to access the variable, and use 'our' when you want to allow external code to access or set the variable.

1
votes

You've already got (and accepted) a answer to this question. But it might be worth raising another couple of points.

Firstly, if you don't understand a Perl error message, then it's often worth adding use diagnostics to your code. That will give you a more detailed explanation of the error. In this case, it would say:

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

(Which, incidentally, shows the small omissions in the previous answer.)

Secondly, are you saying that your book recommends use strict but doesn't mention my? That sounds very strange. In any case, a Perl book that doesn't mention my is not a very good Perl book. Please tell us the title so that we can avoid it.