Perl - Global variable requires explicit package - perl

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.

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.

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.

Related

What is the "once" warnings in perl?

I have code that has,
no warnings 'once';
Reading man warnings I don't see an occurrence of /once/ what does this do?
So long as you don't have strict on, perl allows you to use a variable without declaring it.
perl -wE'$foo = 4;'
Which outputs,
Name main::foo used only once: possible typo at -e line 1.
Note under strict this wouldn't even be permitted,
Global symbol $foo requires explicit package name (did you forget to declare my $foo?) at -e line 1.
You can disable the warning though, without enabling strict by doing no warnings "once"; Though I would suggest strongly you simply remove the unused code instead of silencing the warning.
perl -wE'no warnings "once"; $foo = 4;'
Which both looks ugly and does nothing.
If you run the following you will trigger the warning, plus a little extra explanation:
perl -Mdiagnostics -Mwarnings -e '$foo=1'
The output will be:
Name "main::foo" used only once: possible typo at -e line 1 (#1)
(W once) Typographical errors often show up as unique variable names.
If you had a good reason for having a unique name, then just mention it
again somehow to suppress the message. The our declaration is
provided for this purpose.
NOTE: This warning detects symbols that have been used only once so $c, #c,
%c, *c, &c, sub c{}, c(), and c (the filehandle or format) are considered
the same; if a program uses $c only once but also uses any of the others it
The warning applies to symbol table entries (not "my" lexical variables). If you add -Mstrict to the above, you'll create a strict violation because your variable violates strict 'vars', which prohibits you using a variable that hasn't been declared, with the exception of package globals referred to by their fully-qualified name. If you were to pre-declare $foo with our, the warning goes away:
perl -Mdiagnostics -Mwarnings -Mstrict=vars -E 'our $foo=1'
This works just fine; it avoids a strict violation, and avoids the "once" warning. So the purpose of the warning is to alert you to the use of identifiers that are not declared, not using a fully-qualified name, and also only used once. The objective is to help prevent typos in symbol names, the assumption being that if you use a symbol name only once and have not declared it, it may be a mistake.
Special (punctuation) variables are exempted from this check. You can, therefore, refer to $_ or $/ only once and not trigger a warning. Also, $a and $b are exempt because they are considered special, for use in sort {$a <=> $b} #list; in such constructs they may appear only a single time yet it wouldn't be useful to raise a warning for what is fairly typical code.
You can find the 'once' warning listed in the Warnings Hierarchy here: perldoc warnings.
A list of all diagnostic blurbs is available in perldoc perldiag.

Why does Perl not warn if using an undeclared variable in another namespace - and how can I be warned about this?

I am using strict and warning in my Perl scripts to be notified if I am using undeclared variables. Thus, the interpreter will warn that $foo is undeclared in the following scriptlet:
#!/usr/bin/perl
use warnings;
use strict;
$foo = 'bar';
print ($foo);
However, if I use an undeclared variable in another namespace, I am not warned. The following scriptlet runs without warning whatsoever.
#!/usr/bin/perl
use warnings;
use strict;
$BAR::foo = 'bar';
print ($BAR::foo);
Why is this difference?
Since I have lost quite some time figuring out exactly this problem, albeit in a much larger context, I am wondering if it is possible to make Perl me warn about using undeclared variables in other namespaces, too.
When you fully specify the namespace in which a variable belongs, perl assumes you know what you are doing. See perldoc strict:
strict vars
This generates a compile-time error if you access a variable that was neither explicitly declared (using any of my, our, state, or use vars) nor fully qualified.
I don't think there is a way to detect that you have specified a non-existent variable $BAR::foo. However, if the BAR package is under your control, you can avoid using package variables in the first place by mediating access to the state of foo using accessors, and hiding the variable from other modules.
The answer to problems created by using global variables is not to use global variables.
strict vars
This generates a compile-time error if you access a variable that was neither explicitly declared (using any of my, our, state, or use vars) nor fully qualified.
Perl "trusts" users when they use fully-qualified var names. I suspect it's to allow users to sets config variables in modules that don't use use strict;.
For example, let's look at the following snippet using Data::Dumper:
local $Data::Dumper::Useqq = 1;
print(Dumper($s));
Even long after use strict; was introduced, Data::Dumper didn't declare $Useqq. There wouldn't even have been a mechanism to do so before use strict;! So the above snippet would be using an undeclared variable. That means strict code would not have been able to use Data::Dumper in the above fashion if strict vars was enforced covered fully-qualified names.
It doesn't make sense to prevent strict code from using modules that aren't strict-safe, so strict vars doesn't cover fully-qualified names. These are rare enough and easily-identifiable enough to simply have programmers take more care when using them.
I am wondering if it is possible to make Perl me warn about using undeclared variables in other namespaces, too.
I don't know of existing solutions. It might be possible to hook into Perl to do that, but it would be very hard.
Keep in mind that Perl already warns you if you only use a package variable once, so this should help you catch typos.

How to share a global parameter across 2 different perl scripts

There is a same question on the forum.
How to share/export a global variable between two different perl scripts?
But, the suggestion for that was to use 'our' while declaration and then add 'require' in the other script. When I tried using it, the script fails saying 'Use of reserved word "our" is deprecated'.
Is there a different way to share parameter across different files. Thanks.
If you can't use the our keyword, then you have to fall back to simply using the variable without declaring it properly:
{
no strict;
$variable = "foo"; # implicitly a global
}
The syntax
$MyPackage::variable = "foo"; # uses the fully qualified name
should work too.
These will allow you to use the global, but will not scope the visibility of the name correctly.
However, you should absolutely and immediately upgrade that perl — it is a development release and not a regular release. The current release is perl5 v16.2.

Package name in module code

Short version
Is it possible to access variables from a module declared as our using unqualified names within the BEGIN code block, but using qualified names outside? In particular, can this be done without explicitely naming the package in the module file?
Example
Let demomod.pm be
use strict;
use warnings;
package demomod;
our $foo;
BEGIN { $foo = 42; }
1;
and demoscript.pl be
#!/usr/bin/perl -Tw
use strict;
use warnings;
BEGIN { #INC = ('.', #INC); }
use demomod;
print $demomod::foo."\n";
In this case, all names agree, and everything works as it should. Is there a way to omit the line package demomod; from the demomod.pm code and still let this work?
Motivation
The reason why I'm asking is because I encountered something along these lines during a recent upgrade of Foswiki. That software has a module Foswiki.pm which does not have a package line (EDIT: seems the package line only got lost in my local copy, for reasons unknown). It declares and initializes a variable $engine like in my example. There also is a CGI script called view which sets #INC and then does use Foswiki (); followed by $Foswiki::engine->run(). This last line always fails for me due to the variable not being initialized:
Can't call method "run" on an undefined value at …/view
In the BEGIN block of the module, $engine is set correctly but $Foswiki::engine apparently is not. So it looks like there were two variables here, one qualified and a different one unqualified.
All that code apparently works for others, and a previous version used to work for me as well, without a package line either. So while I try to understand how this broke, I also try to understand how this could work before, without that line in place. Is there some mechanism that would make this work?
If you have no package statement in your code then any package variables will be declared into the main package. So no, you cannot do what you describe.
If you look at line 2 of the Foswiki code that you linked, you will see that it does have a package statement.

Why use strict and warnings?

It seems to me that many of the questions in the Perl tag could be solved if people would use:
use strict;
use warnings;
I think some people consider these to be akin to training wheels, or unnecessary complications, which is clearly not true, since even very skilled Perl programmers use them.
It seems as though most people who are proficient in Perl always use these two pragmas, whereas those who would benefit most from using them seldom do. So, I thought it would be a good idea to have a question to link to when encouraging people to use strict and warnings.
So, why should a Perl developer use strict and warnings?
For starters, use strict; (and to a lesser extent, use warnings;) helps find typos in variable names. Even experienced programmers make such errors. A common case is forgetting to rename an instance of a variable when cleaning up or refactoring code.
Using use strict; use warnings; catches many errors sooner than they would be caught otherwise, which makes it easier to find the root causes of the errors. The root cause might be the need for an error or validation check, and that can happen regardless or programmer skill.
What's good about Perl warnings is that they are rarely spurious, so there's next to no cost to using them.
Related reading: Why use my?
Apparently use strict should (must) be used when you want to force Perl to code properly which could be forcing declarations, being explicit on strings and subs, i.e., barewords or using refs with caution. Note: if there are errors, use strict will abort the execution if used.
While use warnings; will help you find typing mistakes in program like you missed a semicolon, you used 'elseif' and not 'elsif', you are using deprecated syntax or function, whatever like that. Note: use warnings will only provide warnings and continue execution, i.e., it won't abort the execution...
Anyway, it would be better if we go into details, which I am specifying below
From perl.com (my favourite):
use strict 'vars';
which means that you must always declare variables before you use them.
If you don't declare you will probably get an error message for the undeclared variable:
Global symbol "$variablename" requires explicit package name at scriptname.pl line 3
This warning means Perl is not exactly clear about what the scope of the variable is. So you need to be explicit about your variables, which means either declaring them with my, so they are restricted to the current block, or referring to them with their fully qualified name (for ex: $MAIN::variablename).
So, a compile-time error is triggered if you attempt to access a variable that hasn't met at least one of the following criteria:
Predefined by Perl itself, such as #ARGV, %ENV, and all the global punctuation variables such as $. Or $_.
Declared with our (for a global) or my (for a lexical).
Imported from another package. (The use vars pragma fakes up an import, but use our instead.)
Fully qualified using its package name and the double-colon package separator.
use strict 'subs';
Consider two programs
# prog 1
$a = test_value;
print "First program: ", $a, "\n";
sub test_value { return "test passed"; }
Output: First program's result: test_value
# prog 2
sub test_value { return "test passed"; }
$a = test_value;
print "Second program: ", $a, "\n";
Output: Second program's result: test passed
In both cases we have a test_value() sub and we want to put its result into $a. And yet, when we run the two programs, we get two different results:
In the first program, at the point we get to $a = test_value;, Perl doesn't know of any test_value() sub, and test_value is interpreted as string 'test_value'. In the second program, the definition of test_value() comes before the $a = test_value; line. Perl thinks test_value as sub call.
The technical term for isolated words like test_value that might be subs and might be strings depending on context, by the way, is bareword. Perl's handling of barewords can be confusing, and it can cause bug in program.
The bug is what we encountered in our first program, Remember that Perl won't look forward to find test_value(), so since it hasn't already seen test_value(), it assumes that you want a string. So if you use strict subs;, it will cause this program to die with an error:
Bareword "test_value" not allowed while "strict subs" in use at
./a6-strictsubs.pl line 3.
Solution to this error would be
Use parentheses to make it clear you're calling a sub. If Perl sees $a = test_value();,
Declare your sub before you first use it
use strict;
sub test_value; # Declares that there's a test_value() coming later ...
my $a = test_value; # ...so Perl will know this line is okay.
.......
sub test_value { return "test_passed"; }
And If you mean to use it as a string, quote it.
So, This stricture makes Perl treat all barewords as syntax errors. A bareword is any bare name or identifier that has no other interpretation forced by context. (Context is often forced by a nearby keyword or token, or by predeclaration of the word in question.) So If you mean to use it as a string, quote it and If you mean to use it as a function call, predeclare it or use parentheses.
Barewords are dangerous because of this unpredictable behavior. use strict; (or use strict 'subs';) makes them predictable, because barewords that might cause strange behavior in the future will make your program die before they can wreak havoc
There's one place where it's OK to use barewords even when you've turned on strict subs: when you are assigning hash keys.
$hash{sample} = 6; # Same as $hash{'sample'} = 6
%other_hash = ( pie => 'apple' );
Barewords in hash keys are always interpreted as strings, so there is no ambiguity.
use strict 'refs';
This generates a run-time error if you use symbolic references, intentionally or otherwise.
A value that is not a hard reference is then treated as a symbolic reference. That is, the reference is interpreted as a string representing the name of a global variable.
use strict 'refs';
$ref = \$foo; # Store "real" (hard) reference.
print $$ref; # Dereferencing is ok.
$ref = "foo"; # Store name of global (package) variable.
print $$ref; # WRONG, run-time error under strict refs.
use warnings;
This lexically scoped pragma permits flexible control over Perl's built-in warnings, both those emitted by the compiler as well as those from the run-time system.
From perldiag:
So the majority of warning messages from the classifications below, i.e., W, D, and S can be controlled using the warnings pragma.
(W) A warning (optional)
(D) A deprecation (enabled by default)
(S) A severe warning (enabled by default)
I have listed some of warnings messages those occurs often below by classifications. For detailed info on them and others messages, refer to perldiag.
(W) A warning (optional):
Missing argument in %s
Missing argument to -%c
(Did you mean &%s instead?)
(Did you mean "local" instead of "our"?)
(Did you mean $ or # instead of %?)
'%s' is not a code reference
length() used on %s
Misplaced _ in number
(D) A deprecation (enabled by default):
defined(#array) is deprecated
defined(%hash) is deprecated
Deprecated use of my() in false conditional
$# is no longer supported
(S) A severe warning (enabled by default)
elseif should be elsif
%s found where operator expected
(Missing operator before %s?)
(Missing semicolon on previous line?)
%s never introduced
Operator or semicolon missing before %s
Precedence problem: open %s should be open(%s)
Prototype mismatch: %s vs %s
Warning: Use of "%s" without parentheses is ambiguous
Can't open %s: %s
These two pragmas can automatically identify bugs in your code.
I always use this in my code:
use strict;
use warnings FATAL => 'all';
FATAL makes the code die on warnings, just like strict does.
For additional information, see: Get stricter with use warnings FATAL => 'all';
Also... The strictures, according to Seuss
There's a good thread on perlmonks about this question.
The basic reason obviously is that strict and warnings massively help you catch mistakes and aid debugging.
Source: Different blogs
Use will export functions and variable names to the main namespace by
calling modules import() function.
A pragma is a module which influences some aspect of the compile time
or run time behavior of Perl. Pragmas give hints to the compiler.
Use warnings - Perl complains about variables used only once and improper conversions of strings into numbers. Trying to write to
files that are not opened. It happens at compile time. It is used to
control warnings.
Use strict - declare variables scope. It is used to set some kind of
discipline in the script. If barewords are used in the code they are
interpreted. All the variables should be given scope, like my, our or
local.
The "use strict" directive tells Perl to do extra checking during the compilation of your code. Using this directive will save you time debugging your Perl code because it finds common coding bugs that you might overlook otherwise.
Strict and warnings make sure your variables are not global.
It is much neater to be able to have variables unique for individual methods rather than having to keep track of each and every variable name.
$_, or no variable for certain functions, can also be useful to write more compact code quicker.
However, if you do not use strict and warnings, $_ becomes global!
use strict;
use warnings;
Strict and warnings are the mode for the Perl program. It is allowing the user to enter the code more liberally and more than that, that Perl code will become to look formal and its coding standard will be effective.
warnings means same like -w in the Perl shebang line, so it will provide you the warnings generated by the Perl program. It will display in the terminal.