Warning in Perl is the most commonly used Pragma in Perl programming and is used to catch ‘unsafe code’. A pragma is a specific module in Perl package which has the control over some functions of the compile time or Run time behavior of Perl, which is strict or warnings. So the first line goes like this,
When the warning pragma is used, the compiler will check for errors, will issue warnings against the code, and will disallow certain programming constructs and techniques. This pragma sends a warning whenever a possible typographical error and looks for possible problems. A number of possible problems may occur during a program execution but ‘
warnings‘ pragma mainly looks for the most common scripting bugs and syntax errors.
Note: This ‘
use warnings‘ pragma was introduced in Perl 5.6 and later versions, for older versions, warnings can be turned on by using ‘-w’ in the shebang/hashbang line:
use strict pragma also works in the same way as
use warnings but the only difference is that the strict pragma would abort the execution of program if an error is found, while the warning pragma would only provide the warning, it won’t abort the execution.
log file to store the warning:
How to generate a warning
Warnings in Perl can be created by using a predefined function in Perl known as
A warn function in Perl generates a warning message for the error but will not exit the script. The script will keep on executing the rest of the code. Hence, a
warn function is used when there is a need to only print a warning message and then proceed with the rest of the program.
Enabling and Disabling warnings
Warnings can be enabled by using
'use warnings' pragma in the code. However, this pragma can only be used in newer versions of Perl i.e. Version 5.6 or above. For older versions, -w is used to enable warnings. This -w is added in the Hashbang line:
Though this can be used to enable warnings but this ‘-w’ enables the warnings throughout the program, even in the external modules which are being written and maintained by other people.
warning pragma can also be replaced by
use Modern::Perl. This method enables warnings in a lexical scope.
Warnings can be disabled selectively by using ‘
no warning‘ pragma within a scope along with an argument list. If the argument list is not provided then this pragma will disable all the warnings within that scope.
Here in the above code, the assignment of scalar value generates error for assignment to $c but not to $b, because of the
no warnings pragma used within the block.
Creating your own Warnings
Perl allows to create and register your own warnings so that other users can enable and disable them easily in the lexical scopes. This can be done by using a predefined pragma ‘
package Geeks::Perl_program; use warnings::register;
Above given syntax will create a new warnings category named after the package Geeks::Perl_program. Now this warning category is created and can be enabled by using
use warnings 'Geeks::Perl_program' and can be further disabled by using
no warnings 'Geeks::Perl_program'.
To check if the caller’s lexical scope has enabled a warning category, one can use
warnings::enabled(). Another pragma
warnings::warnif() can be used to produce a warning only if warnings are already in effect.
Above example produces a warning in the deprecated category.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.