Perl | Warnings and how to handle them

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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";
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
use warnings;   
  
# Assigning variable to the file
my $filename = 'Helloo.txt';  
  
# Opening the file using open function
open(my $fh, '>', $filename) or warn "Can't open file";     
print "done\n";  

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

use warnings;
my @a;
{
    no warnings;
    my $b = @a[0];
}
  
my $c = @a[0];

chevron_right



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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
package Geeks::Perl_program;
  
use warnings::register;
  
sub import
{
    warnings::warnif( 'deprecated',
        'empty imports from ' . __PACKAGE__ .
        ' are now deprecated' )
    unless @_;
}

chevron_right


Above example produces a warning in the deprecated category.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.