Perl | Warnings and how to handle them
Last Updated :
23 Sep, 2021
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,
use warnings;
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:
#!/usr/local/bin/perl -w
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.
Example:
Perl
#!/usr/bin/perl
use strict;
use warnings;
local $SIG{__WARN__} = sub
{
my $msz = shift;
log1('warning', $msz);
};
my $count1;
count();
print "$count1\n";
sub count
{
$count1 = $count1 + 42;
}
sub log1
{
my ($level, $msg) = @_;
if (open my $out, '>>', 'log.txt')
{
chomp $msg;
print $out "$level - $msg\n";
}
}
Output:
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 'warn'.
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.
Perl
#!/usr/bin/perl
use warnings;
# Assigning variable to the file
my $filename = 'Hello.txt';
# Opening the file using open function
open(my $fh, '>', $filename) or warn "Can't open file";
print "done\n";
Output:

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:
#!/usr/local/bin/perl -w
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.
This 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.
Example:
Perl
use warnings;
my @a;
{
no warnings;
my $b = @a[0];
}
my $c = @a[0];
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 'warnings::register'.
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.
Example:
Perl
#!/usr/bin/perl
package Geeks::Perl_program;
use warnings::register;
sub import
{
warnings::warnif( 'deprecated',
'empty imports from ' . __PACKAGE__ .
' are now deprecated' )
unless @_;
}
Above example produces a warning in the deprecated category.
Similar Reads
How to Disable Python Warnings?
Python warnings are non-fatal messages that alert the user to potential problems in the code. Unlike exceptions, warnings do not interrupt the execution of the program, but they notify the user that something might not be working as expected. Warnings can be generated by the interpreter or manually
3 min read
How to Log Errors and Warnings into a File in PHP?
In PHP, errors and warnings can be logged into a file by using a PHP script and changing the configuration of the php.ini file. Two such approaches are mentioned below: Approach 1: Using error_log() FunctionThe error_log() function can be used to send error messages to a given file. The first argume
3 min read
How to Change Warnings Display in MATLAB?
MATLAB displays warnings whenever there are errors in the execution of a code. These messages vary in detail depending on the method chosen by the user for their warning messages. MATLAB provides two modes of warning messages Verbose BacktraceIn verbose mode, MATLAB gives information on the error as
3 min read
How to Handle the Pylint Message: Warning: Method Could Be a Function
The Pylint is a widely used static code analysis tool for Python that helps maintain the code quality by identifying coding errors enforcing the coding standards and offering the potential code enhancements. One common warning it generates is "Method could be a function." This article will explain t
4 min read
How to handle the warning of file_get_contents() function in PHP ?
The file_get_contents() function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file. The path of the file to
3 min read
Restore Warnings in MATLAB
MATLAB shows warning whenever an error or exception occurs. Generally, these warnings are insightful however, sometimes they are not needed or are needed in a modified state. For such cases, MATLAB provides the option to modify or define one's own warnings for an error/exception. But, there could be
3 min read
Perl | Use of Hash bang or Shebang line
Practical Extraction and Reporting Language or Perl is an interpreter based language. Hash-bangs or shebangs are useful when we are executing Perl scripts on Unix-like systems such as Linux and Mac OSX. A Hashbang line is the first line of a Perl program and is a path to the Perl binary. It allows i
3 min read
How to Handle table Error in R
R Programming Language is commonly used for data analysis, statistical modeling, and visualization. However, even experienced programmers make blunders while dealing with R code. Error management is critical for ensuring the reliability and correctness of data analysis operations. Common causes of t
2 min read
HTTP headers | Warning
The HTTP headers allows the customer and server to pass additional information with an HTTP solicitation or response. The Warning general HTTP header entertains the information about potential problems with the status of the message which might not be reflected in the message. The field itself consi
3 min read
How To Write A Good Bug Report?
A well-written bug report is essential in software testing to facilitate effective communication between testers and developers, leading to improved program quality and user satisfaction. This article explores the key practices for writing thorough bug reports, helping in quick issue identification
8 min read