perlpragma.pod

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· POD 代码 · 共 148 行

POD
148
字号
=head1 NAMEperlpragma - how to write a user pragma=head1 DESCRIPTIONA pragma is a module which influences some aspect of the compile time or runtime behaviour of Perl, such as C<strict> or C<warnings>. With Perl 5.10 youare no longer limited to the built in pragmata; you can now create userpragmata that modify the behaviour of user functions within a lexical scope.=head1 A basic exampleFor example, say you need to create a class implementing overloadedmathematical operators, and would like to provide your own pragma thatfunctions much like C<use integer;> You'd like this code    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";to give the output    A: 4.6    B: 4    C: 4.6    D: 4    E: 4.6I<i.e.>, where C<use myint;> is in effect, addition operations are forcedto integer, whereas by default they are not, with the default behaviour beingrestored via C<no myint;>The minimal implementation of the package C<MyMaths> would be something likethis:    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;Note how we load the user pragma C<myint> with an empty list C<()> toprevent its C<import> being called.The interaction with the Perl compilation happens inside package C<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;As pragmata are implemented as modules, like any other module, C<use myint;>becomes    BEGIN {        require myint;        myint->import();    }and C<no myint;> is    BEGIN {        require myint;        myint->unimport();    }Hence the C<import> and C<unimport> routines are called at B<compile time>for the user's code.User pragmata store their state by writing to the magical hash C<%^H>,hence these two routines manipulate it. The state information in C<%^H> isstored in the optree, and can be retrieved at runtime with C<caller()>, atindex 10 of the list of returned results. In the example pragma, retrievalis encapsulated into the routine C<in_effect()>, which takes as parameterthe number of call frames to go up to find the value of the pragma in theuser's script. This uses C<caller()> to determine the value ofC<$^H{myint}> when each line of the user's script was called, andtherefore provide the correct semantics in the subroutine implementing theoverloaded addition.=head1 Implementation detailsThe optree is shared between threads.  This means there is a possibility thatthe optree will outlive the particular thread (and therefore the interpreterinstance) that created it, so true Perl scalars cannot be stored in theoptree.  Instead a compact form is used, which can only store values that areintegers (signed and unsigned), strings or C<undef> - references andfloating point values are stringified.  If you need to store multiple valuesor complex structures, you should serialise them, for example with C<pack>.The deletion of a hash key from C<%^H> is recorded, and as ever can bedistinguished from the existence of a key with value C<undef> withC<exists>.B<Don't> attempt to store references to data structures as integers whichare retrieved via C<caller> and converted back, as this will not be threadsafe.Accesses would be to the structure without locking (which is not safe forPerl's scalars), and either the structure has to leak, or it has to befreed when its creating thread terminates, which may be before the optreereferencing it is deleted, if other threads outlive it.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?