perlpragma

Perl core documentation, “perlpragma”, public translation into Russian from English More about this translation.

See also 42 similar translations

Translate into another language.

Participants

sharifulin452 points
mikhail.lyubimov255 points
Join Translated.by to translate! If you already have a Translated.by account, please sign in.
If you do not want to register an account, you can sign in with OpenID.
Pages: ← previous Ctrl next
1 2

perlpragma

perlpragma

History of edits (Latest: sharifulin 3 years ago) §

=head1 NAME

=head1 НАЗВАНИЕ

History of edits (Latest: sharifulin 3 years ago) §

perlpragma - how to write a user pragma

perlpragma - Как писать пользовательские прагмы

History of edits (Latest: sharifulin 3 years ago) §

— может собственные? sharifulin

=head1 DESCRIPTION

=head1 ОПИСАНИЕ

History of edits (Latest: sharifulin 3 years ago) §

A pragma is a module which influences some aspect of the compile time or run
time behaviour of Perl, such as C<strict> or C<warnings>. With Perl 5.10 you
are no longer limited to the built in pragmata; you can now create user
pragmata that modify the behaviour of user functions within a lexical scope.

Прагма - это модуль, который влияет на некоторые моменты во время компиляции и во время выполнения Perl-кода. Пример прагмы: C<strict>или C<warnings>. С выходом Perl 5.10 вы не ограничены в разработке прагм, теперь можете создавать пользовательские прагмы, которые будут менять поведение пользовательских функций в лексическом контексте.

History of edits (Latest: sharifulin 3 years ago) §

=head1 A basic example

=head1 Основной пример

History of edits (Latest: sharifulin 3 years ago) §

For example, say you need to create a class implementing overloaded
mathematical operators, and would like to provide your own pragma that
functions much like C<use integer;> You'd like this code

Например, вам нужно создать класс, который реализует перегрузку математических операторов, и вы хотели бы использовать свою прагму с функционалом похожим на C<use integer;>. Пример кода:

History of edits (Latest: sharifulin 3 years ago) §

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";

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";

History of edits (Latest: sharifulin 3 years ago) §

to give the output

Результат выполнения кода:

History of edits (Latest: sharifulin 3 years ago) §

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

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

History of edits (Latest: sharifulin 3 years ago) §

I<i.e.>, where C<use myint;> is in effect, addition operations are forced
to integer, whereas by default they are not, with the default behaviour being
restored via C<no myint;>

I<В примере>, в котором используется C<use myint;>, оператор сложения работает с целыми числами, значения по умолчанию не определены. Поведение по умолчанию будет восстановлено C<no myint;>

History of edits (Latest: sharifulin 3 years ago) §

— не нравится фраза: поскольку ... sharifulin

The minimal implementation of the package C<MyMaths> would be something like
this:

Минимальная реализация пакета C<MyMaths> будет примерно такой:

History of edits (Latest: mikhail.lyubimov 3 years ago) §

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;

package MyMaths;
use warnings;
use strict;
use myint();
use overload '+' => sub {
my ($l, $r) = @_;
# проверка вызова
if (myint::in_effect(1)) {
int($$l) + int($$r);
} else {
$$l + $$r;
}
};

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

1;

History of edits (Latest: sharifulin 3 years ago) §

Note how we load the user pragma C<myint> with an empty list C<()> to
prevent its C<import> being called.

Примечание. При загрузке пользовательской прагмы C<myint> без параметров С<()> функция C<import> не будет вызвана.

History of edits (Latest: sharifulin 3 years ago) §

The interaction with the Perl compilation happens inside package C<myint>:

Взаимодействие с Perl во время компиляции внутри пакета C<myint>:

History of edits (Latest: sharifulin 3 years ago) §

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;

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;

History of edits (Latest: sharifulin 3 years ago) §
Pages: ← previous Ctrl next
1 2