Skip to content

Latest commit

 

History

History
executable file
·
282 lines (196 loc) · 7.5 KB

perlpragma.pod

File metadata and controls

executable file
·
282 lines (196 loc) · 7.5 KB

NAME

perlpragma - ��⓾�若�吟�若����í�違����勖�吾����

DESCRIPTION

�����í�違����壔��strict ��� warnings ��勉�������˨����潟�潟����ゃ��������絎�茵������� Perl �� ��壔����������˨�����腮勉�勐襲��帥��筝���������≪�吾�ャ�若�˨�с����� Perl 5.10 ��������������í�違����夌����粋昭��帥�勉����勉����狗����������障�����; ��㋘�㏍�激�˨� ��鴻�潟�若�������勉�⓾�若�吟�守�∽�違�勖�壔�����������紊���������⓾�若�吟�若����í�違����� 篏���������������˨�ŝ����障��������

A basic example

(��堺�㋜����ŭ��)

箴������違����ŝ�若����若�㏍�若�����������膊�茵�羲�膊�絖����絎�茖���������壔�í�鴻��篏����綽�荀���� ��������������⓾��use integer; ��勉����������������㋝�ŝ�勉����í�違��������箴������������ �����障����� 篁ヤ����勉�������ŝ�潟�若����

use MyMaths;

my $l = MyMaths->new(1.2);
my $r = MyMaths->new(3.4);

print "A: ", $l + $r, "\n";

use myint;
print "B: ", $l + $r, "\n";

{
    no myint;
    print "C: ", $l + $r, "\n";
}

print "D: ", $l + $r, "\n";

no myint;
print "E: ", $l + $r, "\n";

篁ヤ����勉����������阪����������障��

A: 4.6
B: 4
C: 4.6
D: 4
E: 4.6

��ゃ�障�����use myint; ��������鴻�勉�������˨�壠��羈�羲�膊�絖���壩�贋�違��綣桁�吟�������� 筝���鴻�������í�˨����с�壔�������с�壔�������障�������������������í�˨����勖�壔���������� no myint; ��у奨�����������⓾����障�����

MyMaths ��������宴�若�吾�勖��篏������勐��茖���壚札筝���勉�������ŝ����勉�с��:

package MyMaths;
use warnings;
use strict;
use myint();
use overload '+' => sub {
    my ($l, $r) = @_;
        # Pass 1 to check up one call level from here
    if (myint::in_effect(1)) {
        int($$l) + int($$r);
    } else {
        $$l + $$r;
    }
};

sub new {
    my ($class, $value) = @_;
    bless \$value, $class;
}

1;

import �����若�喝�冴�������ŝ����������˨�⓾�若�吟�若����í�違�� myint ���腥冴�ŝ�鴻�� () 篁������у�若�喝�冴����号�����羈���������⓾��������������

Perl ��潟�潟����ゃ�í����勛�娯��篏������壔�������宴�若�� myint ��勐����眼�ц儀��������障��:

package myint;

use strict;
use warnings;

sub import {
    $^H{myint} = 1;
}

sub unimport {
    $^H{myint} = 0;
}

sub in_effect {
    my $level = shift // 0;
    my $hinthash = (caller($level))[10];
    return $hinthash->{myint};
}

1;

篁���勉�≪�吾�ャ�若�˨�����罕���������í�違�������≪�吾�ャ�若�˨�������☗��茖����������use myint; �� 篁ヤ����勉�������˨�ŝ��:

BEGIN {
    require myint;
    myint->import();
}

no myint; ��壚札筝���勉�������˨�ŝ����障��:

BEGIN {
    require myint;
    myint->unimport();
}

緇���c�� import ��� unimport ��勉�˨�若����潟�壔�⓾�若�吟�若�潟�若���� ��潟�潟����ゃ����� �����若�喝�冴�������障�����

��⓾�若�吟�若����í�違����壔�������吾�˨�˨�ŝ�������激�� %^H �����吾��莨若����������х�倶����� 篆������������勉�с�������������勌����ゃ�勉�泣����˨�若����潟�壔�����������篏������障����� %^H ��勛�倶�������宴�壩����������˩��膊<��������caller() ������菴������������ŝ�鴻���� ��ゃ�潟�������壔�� 10 ��勤��膣���������⓾����☗��茵�筝㏍�����緇���с����障����� 箴���勉����í�違����с�壔��菴���������������勉�壔�⓾�若�吟�若�鴻�壔�ŝ�������勉����í�違����勐�ゃ�� 荀���ゃ�������������˩�������c�⓾�������若�喝�冴�������㋘�若����勖�違�������í�<�若�帥�������☗����������� in_effect() ��˨�若����潟�с�˨����祉�������������障����� ��������壔�⓾�若�吟�若�勉�鴻�壔�ŝ�������勐��茵������若�喝�冴����������������� $^H{myint} �� ��ゃ��羆阪����������������� caller() ���篏帥����勉�с����ŝ�若����若�㏍�若��������������膊���� 絎�茖������⓾�������泣����˨�若����喝����ф③�����������活��������箴������障�����

Implementation details

(絎�茖���勤¼膣�)

罕���������壔�鴻�㋘����������у�掩����������障����� ��ゃ�障�����罕���������壔��������篏���c����劫����勉�鴻�㋘�����(緇���c�⓾�ゃ�潟�帥����ŝ�水��篏�)��������� ��欠������������櫝�醇�с����������◑�������������с����� 緇���c�⓾�������� Perl ��鴻�˨�í��罕���������˩��膊<����������������堺�ャ�障�������� ��х軒綵√�����篏帥��篁c�������˨����贋��(膃☗垬�������◐❻�垩�ŝ��)������絖�������undef �� ��������������勐�ゃ�勉�帥��篆�膊<�с����障�� - ��ŝ����<�㋘�潟�鴻��羌勐��絨���亥�号�違� ���絖���������������障����� ������茲���違�勐�ゃ��茲������ʋ�����篏����篆�膊<�����綽�荀������������ŝ�����箴������� pack ��ŝ�í�� 篏帥�c�☎�翫�������������鴻����с����� %^H ��������勉�������激�ャ�㏍�若�勐����ゃ�虁����蚊�������������障�障�ч����� exists ���篏帥���������� ��ゃ�� undef ��с�㏍�若��絖���������������������阪�ャ�с����障�����

��ŝ����<�㋘�潟�鴻��caller 腟���宴�у��緇������☗����������眼�������贋�違�������⓾����若�炊������� 篆�膊<����������������⓾�壔�������障�����; ��������壔�鴻�㋘�������祉�若����с�壔�ŝ����������с����� ��≪�壔�祉�鴻�壔�㏍����壔�ŝ����с�勖�����篏����絲障����⓾�с�����(��������� Perl ��勉�鴻�˨�í��絲障����⓾� 絎������с�壔�������障�����)���罕����篏������<�≪�ŝ�ŝ�若�壔�����������篏������������鴻�㋘��������腟�篋������ 茹f�障��������������������篁���勉�鴻�㋘����������欠�����������������������罕���������������ゃ�������������勉�� �����с�������������˨�ŝ����障�����