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.
- Merge Conflicts and How to handle them
- How to log errors and warnings into a file in php?
- How I Handle GATE CS Preparation with College Studies
- How to handle the warning of file_get_contents() function in PHP ?
- How to handle errors in node.js ?
- How to handle events in dynamically created elements in jQuery ?
- Web Browsers that support Java Applets and how to enable them
- How to target all Font Awesome icons and align them center?
- Perl | Constructors and Destructors
- Perl | Opening and Reading a File
- Perl | STDIN in Scalar and List Context
- Perl List and its Types
- Perl | Quoted, Interpolated and Escaped Strings
- Perl | Number and its Types
- Perl | Hash in Scalar and List Context
- Perl | Grouping and Alternation in Regex
- Perl | Date and Time
- Perl | Greedy and non-greedy match
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.