In my pervious question I asked how to use multiple modules with one use. Got one perfect answer, and another one what pointed me to Modern::Perl module what is really simple.
After a bit searching CPAN I found another module called uni::perl, what is really complicated - it is and equivalent for:
use strict;
use feature qw(say state switch);
no warnings;
use warnings qw(FATAL closed threads internal debugging pack substr malloc
unopened portable prototype inplace io pipe unpack regexp
deprecated exiting glob digit printf utf8 layer
reserved parenthesis taint closure semicolon);
no warnings qw(exec newline);
use utf8;
use open (:utf8 :std);
use mro 'c3';
Can someone explain/comment it how its works?
I pasted the whole code here divided into few segments and added my questions into, (with ###
).
I understand than this question is really long. But, dividing it into smaller one will not help, because the whole is about the "uni::perl" module.
Please, help me understand the problematic parts.
package uni::perl;
use 5.010;
BEGIN {
### OK - these are bitmask of different warnings, they're coming from:
# paste this into perl to find bitmask
# no warnings;
# use warnings qw(FATAL closed threads internal debugging pack substr malloc unopened portable prototype
# inplace io pipe unpack regexp deprecated exiting glob digit printf
# utf8 layer reserved parenthesis taint closure semicolon);
# no warnings qw(exec newline);
# BEGIN { warn join "", map "\\x$_", unpack "(H2)*", ${^WARNING_BITS}; exit 0 };
${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
$^H |= 0x00000602; ### this mean "use strict;"
}
Setting directly the ${^WARNING_BITS}
an the $^H
, is faster than a common "use strict" and etc?
What doing this m{ }x
.
m{
use strict;
use warnings;
}x;
use mro ();
I know the "match" operator and 'x' flag but not understand what is doing in this context.. use mro
is some "dark-magic" what probably an common perl users don't need to know... ;)
What's do the local *__ANON__
line? For what is good the goto
in this context?
The whole next BEGIN block is an dark magic for me. ;(
BEGIN {
for my $sub (qw(carp croak confess)) {
no strict 'refs';
*$sub = sub { ### for what need replace the global *croak (etc) with this sub?
my $caller = caller;
local *__ANON__ = $caller .'::'. $sub; ### what's mean this?
require Carp;
### This set the Carp code-refs to the global namespace?
### But who is the "caller" in the BEGIN block? (compile time)
*{ $caller.'::'.$sub } = \&{ 'Carp::'.$sub };
goto &{ 'Carp::'.$sub }; ### Why need goto here?
};
}
}
Finally - some clearer things. Rewrite the import
so, this will be called when use uni::perl;
sub import {
my $me = shift;
my $caller = caller;
### OK - again the bitmasks
${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
### where are these documented?
$^H |=
0x00000602 # strict
| 0x00800000 # utf8
;
# use feature
$^H{feature_switch} =
$^H{feature_say} =
$^H{feature_state} = 1;
# use mro 'c3';
mro::set_mro($caller, 'c3');
#use open (:utf8 :std);
${^OPEN} = ":utf8\0:utf8";
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
### again coderef magic. As I understand it - it will replace the
### "carp, etc" in the callers namespace with the coderef's defined
### in the above BEGIN block. But why with this complicated way?
for my $sub (qw(carp croak confess)) {
no strict 'refs';
*{ $caller .'::'. $sub } = \&$sub;
}
### and finally - I have abosolutely no idea - what do the next code
### will take arguments of "use uni::perl qw(arg)"
### but have no idea how to use it - or what is doing ;(
while (@_) {
my $feature = shift;
if ($feature =~ s/^://) {
my $package = $me. '::'. $feature;
eval "require $package; 1" or croak( "$@" );
$package->load( $caller );
}
}
}
what is doing the last while
?
Plus question:
Because this question has more parts, please, quote the relevant part when you giving an answer.
THANK YOU ALL IN ADVANCE.
Setting the warning bits directly is a bit faster, and has more predictable behavior (you can see everything that should happen), but it is obviously much harder to work with and maintain. It may be that the set of warnings that uni::perl
is trying to load is easier done through the bitmasks.
m{ use strict; use warnings;}x;
is simply a regex in void context. It would throw an error either about context or $_
not being set if warnings were enabled. I am not sure exactly why this is being done, it might be to appease some code metric system that looks for the lines "use warnings; use strict". I would have probably written it q{...} if 0;
which is at least a little clearer.
This BEGIN block is creating custom versions of the functions in Carp. It is using the local *__ANON__ = ...
line to set the name of any anonymous subroutines so that the Carp stack trace is easier to follow. The BEGIN block creates the wrapped Carp routines. The import subroutine then loads these new wrapped routines into the caller's namespace.
That last while seems to be loading additional plugin modules for uni::perl
.
The same thing is not being done, see the answer to #3. (BEGIN
creates the wrapped routines, import
installs them into the caller's space)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With