Skip to content

Latest commit

 

History

History
executable file
·
1009 lines (703 loc) · 25.1 KB

perllexwarn.pod

File metadata and controls

executable file
·
1009 lines (703 loc) · 25.1 KB

NAME

perllexwarn - Perl ��勉�㋘�㏍�激�˨�˨�ʃ⑥���

DESCRIPTION

use warnings �����í�違����壔��Perl �����㏍�違�í����勉��������������絲障����⓾�í�勤⑥������ �����鴻�˨�����������膕上�������九勝��с�������������˨����障����� ��������壔�潟����潟����í�ゃ�潟�ŝ����激�с�� -w ��������括��箴<�� Perl 紊���� $^W ��������� ���������荵���ŭ撮��炊����с�����

�����勉����í�違����壔�<�������� strict �����í�違��������罕�������篏������障����� ��ゃ�障�����茘☗�������í�違����勉�鴻�潟�若����奝�������������㏍����壠����̹��絎���������障����� ��障����������í�違��荐㊤����� (use, require, do ������������)�����<�ゃ�˨��莇������ 羲闆í�������������壔�������障�������� ��������˨����������≪�吾�ャ�若�˩�������虁⑥��������с����壔�勐墾�����������㋜���� 荐㊤����с�������������˨�ŝ����障�����

��������í�˨����с�壔����ŝ����激�с�潟�勤⑥�����夌�≦�鴻�ŝ�勉�с��茘☗�������九勝�����������������ŝ�� ��㋘�㋘�激�若�潟�若����壠����眼�ŝ����у��篏������障�����

�����������㏍����壠����у����⓾�勤⑥�����������鴻�˨�������˨�壚札筝���勉�í�<�������勉�������˨����障��:

use warnings;
use warnings 'all';

���罕���˨�������������㏍����壠����у����⓾�勤⑥��������≦�鴻�˨�������˨�壚札筝���勉�í�<�������勉������� �����障��:

no warnings;
no warnings 'all';

箴������違��篁ヤ����勉�潟�若�������������障��:

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

紊���眼�勉����㏍����壔�с�虁⑥�����壩����鴻�с�������������眼�勉����㏍����壔�с�夌�≦�鴻�с����� �����勐�翫�������鴻�˨�� $c ��吾�勌撮��ャ�с� "Scalar value @a[0] better written as $a[0]" 茘☗�������冴�障�������� ��鴻�˨�� $b ��吾�勌撮��ャ�с�壠�冴�障��������

Default Warnings and Optional Warnings

(��������í�˨����勤⑥��������ŝ����激�с�潟�勤⑥���)

��㋘�㏍�激�˨�̬⑥�����勤ą�����勐����˨��Perl ��壚����ゃ�勤⑥�����壔�í�鴻����������障��: 綣桁�句��(mandatory)�����ŝ����激�с��(optional)��с�����

���������腓冴����⓾�������������˨����潟�若�����綣桁�句����ʃ⑥������綣���c���������������������� �����障�ʋ����潟�˨��������������茘☗�������阪�������障����� 箴������違��篁ヤ����勉�潟�若����� "2:" ��勰��������絲障����☗幻��� "isn't numeric" 茘☗����� ��阪�������障�����

my $a = "2:" + 3;

��㋘�㏍�激�˨�˨�ʃ⑥�����勐����ャ�˨����c�⓾��綣桁�句����ʃ⑥������ ��������í�˨����� 茘☗���� ��ŝ����障�������� ��������壔��篁ュ����勐七��句����ʃ⑥�����壚����с����������í�˨����ф����鴻�с��������綣����膓��� ��㋘�㏍�激�˨�˨�ʃ⑥��������í�違����ф�����/��≦�鴻�˨����堺�ャ����������с����� 箴������違��篁ヤ����勉�潟�若����с�壔��"isn't numeric" 茘☗����� $a 紊���違��絲障����⓾����� ��怨����������障�����

my $a = "2:" + 3;
no warnings;
my $b = "2:" + 3;

-w ��ŝ����激�с�潟�� $^W ��壔�������í�˨����勤⑥��������≦��/�����鴻�˨�������勉�˨� 篏帥����ŝ�����������羈���������⓾�������������� �����勐�翫����壠七��句����ŝ�障�障�с�����

What's wrong with -w and $^W

(-w ��� $^W ��勌�������ŝ�����?)

茘☗����������鴻�˨�������勉�˨�潟����潟����í�ゃ�潟�� -w ���篏帥�������������勉�壔����⓾�� 箴水�í�с����������ŝ�若�˨�ŝ�≪�������激�潟�違�с����������������馹������������障����� Perl ��勉����㏍�違�í�������吾����⓾�������������勉�������������倶�������������障����� ��潟�若����勌�������壔����ŝ����ʃ昆�����吾����障�������������ŝ��腆阪�������≪����吾�������⓾����� Perl ��≪�吾�ャ�若�˨����ñ�������障����� �����勉�������ɱ�翫����� -w �����í�違��篏帥�������������ŝ�������吾����⓾����ŝ����潟�若���� 絲障����⓾��茘☗����������鴻�˨�������������˨�ŝ����障�����

���罕���˨����潟�若�������㏍����壔�ф����鴻�障����夌�≦�鴻�˨�������������� $^W ���篏帥����������˨�� ��㋝蟹�����ʋ����鴻����������障����� ��障�������潟�若�������㏍����壔�ц⑥��������≦�鴻�˨����������������障����� 篁ヤ����勉�������˨�������医����������◑����������������������障�����:

{
    local ($^W) = 0;
         my $a =+ 2;
         my $b; chop $b;
}

�����勉�潟�若����� -w �����í�遺�������у��茵����������������$a ��勤����ц⑥������ ��冴�障��: "Reversed += operator"���

��馹���壔��Perl ��˨�壔�潟�潟����ゃ�����茘☗�����絎�茵����茘☗�������������◑�������������с����� ��潟�潟����ゃ�����茘☗�������≦�鴻�˨�������˨�壔��篁ヤ����勉�������˨�潟�若�������吾����眼��綽�荀���� ��������障��:

{
    BEGIN { $^W = 0 }
         my $a =+ 2;
         my $b; chop $b;
}

$^W ��̹�≪�����������筝���ゃ�勐�馹���壔����潟�若��筝㏍�勌����喝����勌��臀勉�勤┃絎���т���������� 茘☗��荐㊤�����紊������������������������с����� 箴������違��篁ヤ����勉�潟�若�����(-w �����í�違�ŝ�����)絎�茵����������������doit �� 2 �����勉�勐�若�喝�冴����� "Use of uninitialized value" 茘☗�������冴�障�������� 1 �����勉�с�壠�冴�障��������

sub doit
{
    my $b; chop $b;
}

doit();

{
    local ($^W) = 1;
    doit()
}

��������� $^W �����������鴻�潟�若����������ゃ�������勐�壚�������с�����

��㋘�㏍�激�˨�˨�ʃ⑥�����壔����í����ц⑥������綣���c�����������綣���c����������ŝ�������̹�≪���� ������膕上墾��勰�������九勝�����������������с�������������勐�狗����������帥����障�����

Controlling Warnings from the Command Line

(��潟����潟����í�ゃ�潟�����茘☗�������九勝������)

�����よ⑥��������榊��������(�����������夌�榊�������ŝ��)��������九勝��������������˩戎��������� 筝���ゃ�勉�潟����潟����í�ゃ�潟����í�違����������障��:

-w

��������壩�√����勉����í�違�с����� ��㋘�㏍�激�˨�̬⑥��������í�違����������ŝ����勉�潟�若����������ŝ�����篏帥�c�⓾�������≪�吾�ャ�若�˨� ��í����с�� 篏帥�������⓾����ŝ�� ��ŝ����������勉����í�違�壠����⓾�勐�贋����ц⑥������ �����鴻�˨����障����� �����勉����í�違����㋘�㏍�激�˨�̬⑥��������í�勉�������̥�娯��篏���������������̹�≪�����荅括完��˨�ゃ����⓾� "Backward Compatibility" ��������с����⓾��������������

-W

��潟����潟����í�ゃ�潟�� -W �����í�違��篏帥�������������������㏍�違�í��筝㏍� no warnings ��� $^W =0 ���篏帥�c�☀⑥��������≦�鴻�˨����⓾����⓾����∴�������⓾�������⓾� 茘☗����������鴻�˨����障����� ��������� use, require, do 腟���宴�ц˛��粋昭��障����������⓾�勉����<�ゃ�˨�˨�� ��ñ����������障����� Perl ������ "lint" ��潟����潟����勛��箴∞�í�◑�������������障�����

-X

罩g∈��� -W �����í�違�勰�����茵������障��; ��ゃ�障����������⓾�勤⑥��������≦�鴻�˨����障�����

Backward Compatibility

(緇���剛��������)

��㋘�㏍�激�˨�˨�鴻�潟�若��茘☗�����絨���ャ�������������勉����若�吾�с�潟�� Perl ��у��篏���������⓾����������� ��㋘�㏍�激�˨�̬⑥������ $^W ��勌検��鴻�勉�潟�若�������������翫����������勛����壔����������� ��í�勉�������̥�娯��篏����������������荐�菴違����⓾����障�����

��㋘�㏍�激�˨�̬⑥������ -w/$^W ��勛�娯��篏����:

  1. 茘☗�������九勝������筝���ゃ�勉�潟����潟����í�ゃ�潟����í�� (-w, -W or -X) �� ��í�����篏帥�������⓾�����������$^W ��� the warnings �����í�違�����篏帥�������⓾����ŝ�� ��翫�������������í�˨����勤⑥�����������鴻�˨�ŝ�������ŝ����激�с�潟�勤⑥�����夌�≦�鴻�˨�ŝ����障����� ��������˨��������茘☗�������九勝�����������������ŝ����㋘�㋘�激�若�潟�若����夌�≦����眼�у��篏������障�����

  2. 5.005 ������ -w �����í�違�壔�違�㏍�若����˨�� $^W 紊���違��荐㊤�������障����� ��������˨��������茘☗����勖�壔�������������九勝��������������� $^W ������篏��������������� 箴�絖������⓾�������㋘�㋘�激�若�潟�若����壔����勉�障�障�у��篏������障�����

  3. �����遵�ゃ�˨�ŝ�c����������壠�ャ�������⓾��$^W 紊���違�壩③腆冴������������������� ��九勝筝���純�ŝ�違�㏍�若����˨�ʋ�号����ф��篏������障����������������í�˨����勤⑥�����������劫��/ ��≦�劫����������������壠�堺�ャ�障��������

  4. ��潟�若�������� warnings �����í�違����勐�九勝筝���˨�������翫�����$^W 紊���違� -w �����í�違�勌検��鴻�壔�㋘�㏍�激�˨�̬⑥�����勉�鴻�潟�若����х�∴����������障�����

  5. ��㋘�㏍�激�˨�̬⑥���荐㊤�����筝���吾����������壚����勖�号����� -W ��障����� -X ��潟����潟����í�ゃ�潟����í�違��篏帥����������с�����

3 & 4 ��勛����水����������勐�号����˨����������㋚����̬⑥��������������������� $^W �����勉�潟�若���� 茘☗����勖�壔����������� (local $^W=0 ���篏帥�c��) ��九勝�������������� warnings �����í�違�����篏帥����障�������������壔�с����障��������

Category Hierarchy

(��˨����眼�ʒ��絮�)

�����˨����眼�ŝ����勰��絮ゃ�壔��茘☗����勉�違�˨�若����������≪����☀⑥������������/��≦�鴻�˨�с������������� ���������������絎�臂í�������⓾����障�����

�憜����勰��絮ゃ��:

all -+
     |
     +- closure
     |
     +- deprecated
     |
     +- exiting
     |
     +- experimental --+
     |                 |
     |                 +- experimental::lexical_subs
     |
     +- glob
     |
     +- imprecision
     |
     +- io ------------+
     |                 |
     |                 +- closed
     |                 |
     |                 +- exec
     |                 |
     |                 +- layer
     |                 |
     |                 +- newline
     |                 |
     |                 +- pipe
     |                 |
     |                 +- unopened
     |
     +- misc
     |
     +- numeric
     |
     +- once
     |
     +- overflow
     |
     +- pack
     |
     +- portable
     |
     +- recursion
     |
     +- redefine
     |
     +- regexp
     |
     +- severe --------+
     |                 |
     |                 +- debugging
     |                 |
     |                 +- inplace
     |                 |
     |                 +- internal
     |                 |
     |                 +- malloc
     |
     +- signal
     |
     +- substr
     |
     +- syntax --------+
     |                 |
     |                 +- ambiguous
     |                 |
     |                 +- bareword
     |                 |
     |                 +- digit
     |                 |
     |                 +- illegalproto
     |                 |
     |                 +- parenthesis
     |                 |
     |                 +- precedence
     |                 |
     |                 +- printf
     |                 |
     |                 +- prototype
     |                 |
     |                 +- qw
     |                 |
     |                 +- reserved
     |                 |
     |                 +- semicolon
     |
     +- taint
     |
     +- threads
     |
     +- uninitialized
     |
     +- unpack
     |
     +- untie
     |
     +- utf8 ----------+
     |                 |
     |                 +- non_unicode
     |                 |
     |                 +- nonchar
     |                 |
     |                 +- surrogate
     |
     +- void

"strict" �����í�違��������罕���������������勉�˨����眼�ŝ�夌����水����������������������с����障��

use warnings qw(void redefine);
no warnings qw(io syntax untie);

��������� "strict" �����í�違��������罕�����憜����勉�鴻�潟�若����̬����違� warnings �����í�違����勐��篏������������������壔����号����壠��膊���������障�����

use warnings qw(void); # only "void" warnings enabled
...
use warnings qw(io);   # only "void" & "io" warnings enabled
...
no warnings qw(void);  # only "io" warnings enabled

��������劫����勤⑥��������í�勉�˨����眼�ŝ����蚊��綵���⓾�������⓾�������������ャ����˨� perldiag ��������с����⓾��������������

羈����: Perl 5.6.1 ��с�壔����㋘�㏍�激�˨�̬⑥�����˨����眼�� "deprecated" ��� "syntax" ��˨����眼�ŝ�勐�壔�˨����眼�ŝ�с�������� 篁���с�壔�������ŭ����с����������㋘����˨�˨����眼�ŝ�с�����

Fatal Warnings

(��翫�順��茘☗��)

��˨����眼�ŭ��荀т賢��� "FATAL" ��勖��絖������������������㋘�㏍�激�˨�˨�鴻�潟�若���� ���絎������������˨����眼�ŝ�ф����冴�������������⓾�勤⑥��������翫�順�������í�若�������若�������障����� 篁ヤ����勉�潟�若����с�壔��time, length, join ��勌戎�����壠���� "Useless use of xxx in void context" 茘☗�������阪�������障�����

use warnings;

time;

{
    use warnings FATAL => qw(void);
    length "abc";
}

join "", 1,2,3;

print "done\n";

絎�茵�������������篁ヤ����勐�阪����������������障��

Useless use of time in void context at fatal line 3.
Useless use of length in void context at fatal line 7.  

length ���篏帥�������⓾�������鴻�潟�若����с�� void 茘☗����˨����眼�ŝ����翫�順�������í�若� �����若����������勉�с�������勤⑥��������坂�������������㏍�違�í����夌�眼�<�̥��篋������障�����

���腓榊����� "FATAL" 茘☗�������ŝ����˨�������˨�壔�������̹�∫�c�����茘☗�������≦�鴻�˨����障����� ��������с��箴������違��筝�菴違�勌����� "void" 茘☗�������≦�鴻�˨�������˨�壔��篁ヤ����勌����ゃ� �����勉�í�<��������篏帥����障��:

no warnings qw(void);
no warnings FATAL => qw(void);

��翫�順�������í�若�������若�����茘☗��������絽吾�勤⑥�����̹����若�������������翫����� "NONFATAL" �����若����若�í��篏帥����障����� 箴������違��篁ヤ����勉�潟�若����� "syntax" ��˨����眼�ŭ札紊���勐����⓾�勤⑥��������翫�順�������í�若� �����若�������障�����

use warnings FATAL => 'all', NONFATAL => 'syntax';

Reporting Warnings from a Module

(��≪�吾�ャ�若�˨�����茘☗�������怨��������)

warnings �����í�違����壔�≪�吾�ャ�若�˩�������˨����c�〠�������ŝ�������ゃ����勰�∽�違�� ���箴������障����� warnings �����í�違��腟���宴�ф����鴻�˨�ŝ�c����≪�吾�ャ�若����堺����勤⑥��������若�喝�冴������� ��怨����������������˩戎��������障�����

篁ヤ����� MyMod::Abc ��≪�吾�ャ�若�˨����������障�����

package MyMod::Abc;

use warnings::register;

sub open {
    my $path = shift;
    if ($path !~ m#^/#) {
        warnings::warn("changing relative path to /var/abc")
            if warnings::enabled();
        $path = "/var/abc/$path";
    }
}

1;

warnings::register ��勐�若�喝�冴����˨��������"MyMod::Abc" �����������������勖�違����� 茘☗����˨����眼�ŝ��篏���������障��; ��ゃ�障�������違�������˨����眼�ɱ����夌憜����勉�������宴�若�後���� 筝���眼����障����� �����勉�≪�吾�ャ�若�˨�� open ��∽�違�壔��綣���違�������☎�後嚳�����鴻��筝��������������� 茘☗����<����祉�若�吾����阪�������障����� �����勤⑥�����壔��MyMod::Abc ���篏帥����潟�若����� 篁ヤ����勉�������˨���� warnings ��˨����c�〠����鴻�˨����������翫����˨�勉�水�阪����������障�����

use MyMod::Abc;
use warnings 'MyMod::Abc';
...
abc::open("../fred.txt");

��障�����warnings::enabled ��∽�違��篏帥�c�⓾����≪��絎�臂í�������⓾�������˨����眼�ŝ�� ��若�喝�冴����≪�吾�ャ�若�˨�ц┃絎���������⓾����������í�������������鴻������������������櫝�純�с����� 篁ヤ����勉�潟�若����������������障��:

package MyMod::Abc;

sub open {
    warnings::warnif("deprecated", 
                     "open is deprecated, use new instead");
    new(@_);
}

sub new
...
1;

open ��∽�違�奝���ィ絅���ŝ�勉�с����若�喝�冴����≪�吾�ャ�若�˨�� (絨���ŝ��������) "deprecated" 茘☗����˨����眼�ŝ�������鴻�勉�����茘☗�������阪����������潟�若�������˨�障����⓾����障����� ��ゃ�障����������ʋ�������с�����

use warnings 'deprecated';
use MyMod::Abc;
...
MyMod::Abc::open($filename);

絎������̬⑥�����<����祉�若�吾����阪����������˨�壔��warnings::warn ��∽�違� warnings::warnif ��∽�違�勉�í�<��������篏帥����鴻����с����� ��������壔��茘☗�������翫�順�������í�若�������若��������罘���純��篏帥�������������˨�������������с����� ��������с�������勉�������ɱ�翫��:

use MyMod::Abc;
use warnings FATAL => 'MyMod::Abc';
...
MyMod::Abc::open('../fred.txt');

warnings::warnif ��∽�違�壔��������罎���冴����⓾��茘☗�������阪��������緇� die �����障�����

筝���ゃ�勤⑥�����∽�� warnings::warn, warnings::warnif, warnings::enabled ��壔����ŝ����激�с�潟�������⓾�˨����眼�ɱ����勌撮��������˨�ŝ����吾�с�壔���� ��ŝ����<�㋘�潟�鴻�������������������с����障����� �����勐�翫����∽�違�虁⑥�����˨����眼�ŝ�������⓾�ŝ����吾�с�壔����勉�壔�í�劫�����篏帥����障�����

�����勌�������������障��:

package Original;

no warnings;
use warnings::register;

sub new
{
    my $class = shift;
    bless [], $class;
}

sub check
{
    my $self = shift;
    my $value = shift;

    if ($value % 2 && warnings::enabled($self))
      { warnings::warn($self, "Odd numbers are unsafe") }
}

sub doit
{
    my $self = shift;
    my $value = shift;
    $self->check($value);
    # ...
}

1;

package Derived;

use warnings::register;
use Original;
our @ISA = qw( Original );
sub new
{
    my $class = shift;
    bless [], $class;
}

1;

篁ヤ����勉�潟�若����壚検��鴻�勉�≪�吾�ャ�若�˨��篏帥�c�⓾����障��������Derived ��������勤⑥������������ �����鴻�˨����⓾����障�����

use Original;
use Derived;
use warnings 'Derived';
my $a = Original->new();
$a->doit(1);
my $b = Derived->new();
$a->doit(1);

�����勉�潟�若����� Derived ��ŝ����吾�с�壔����������勉�水��茵���������⓾�������������� $b ��虁⑥��������阪�������障�����

Odd numbers are unsafe at main.pl line 7

��ŝ����吾�с�壔�������������˩戎���������茵���ц⑥��������怨�������������������˨��羈���������⓾��������������

茘☗����勖�違�������˨����眼�ŝ����脂�蚊��������������篁ヤ����勉�������˨����� warnings::register �� ��������ŝ��������������箴���с����障��:

package MyModule;
use warnings::register qw(format precision);

...

warnings::warnif('MyModule::format', '...');

SEE ALSO

warnings, perldiag

AUTHOR

Paul Marquess