1. Perl
  2. Module
  3. here

warnings module

Use the warnings pragma to display warnings.

use warnings;

In Perl, in the default state, no warning is displayed and the script is executed. You can use the warnings pragma to display warnings. When writing a script, be sure to write it together with the strict pragma.

Frequently seen warning

Here are some of the warnings you'll see often.

Use undefined values

A warning is displayed if you use an undefined value. In the following script, the undefined value is output as it is by the print function.

use warnings;

# Use undefined value
my $str;
print $str;

You will see a warning similar to the following:

Use of uninitialized value $str in print at a.pl line 6

Since it is in English, I will translate it.

I am using the undefined value $str in print. 6th line.

If you get a warning that you are using undefined, it means that the variable is left unassigned. In other words, there is a mistake in the script. Let's modify the script so that this warning does not appear.

String is not encoded

When using multibyte characters such as Japanese, you may see a warning that the string is unencoded. Below is the script that raises the warning.

use warnings;
use utf8;

# String is not encoded
my $str = 'Hello';
print $str;

You will see a warning similar to the following:

Wide character in print at a.pl line 7.

Since it is in English, I will translate it.

Wide characters are used. 7th line.

This means that the string is being output unencoded. So let's encode it properly.

use warnings;
use utf8;
use Encode 'encode';

# String is not encoded
my $str = 'Hello';
print encode('UTF-8', $str);

Subroutines have been redefined

If you define a subroutine more than once, you will be warned that the subroutine has been redefined. Below is the script that raises the warning.

use warnings;

# Subroutine redefinition
sub one {1}
sub one {1}

You will see a warning similar to the following:

Subroutine one redefined at a.pl line 5.

Since it is in English, I will translate it.

Subroutine one has been redefined. 5th line.

When redefining a subroutine, it's usually likely that you've made a mistake, but sometimes you want to temporarily replace the subroutine. In such cases, you can suppress this warning.

no warnings'redefine';

This definition eliminates the subroutine redefinition warning.

use warnings;
no warnings'redefine';

# Subroutine redefinition warning does not occur
sub one {1}
sub one {1}

Warnings other than subroutine redefinition warnings can be suppressed in a similar way, but in principle warnings should not be suppressed. You should fix the part that caused the warning.

Suppress subroutine redefinition warnings

If you have a bug during development and need to fix it in a hurry, you will sometimes apply a monkey patch. A monkey patch is to overwrite the definition of a subroutine or method so that it behaves correctly.

But when I redefine the subroutine, I get a warning. To prevent this warning from appearing, use the warnings pragma to instruct the compiler to stop the subroutine redefinition warning.

no warnings'redefine';

The warning will no longer be displayed. It has a lexical scope. Below is an example that overrides the module's methods.

Related Informatrion