Skip to content

Commit 1f881ae

Browse files
committed
Add B::Deparse->ambient_pragmas_from_caller helper shortcut
1 parent 4068be0 commit 1f881ae

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

lib/B/Deparse.pm

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# This is based on the module of the same name by Malcolm Beattie,
88
# but essentially none of his code remains.
99

10-
package B::Deparse 1.87;
10+
package B::Deparse 1.88;
1111
use strict;
1212
use Carp;
1313
use B qw(class main_root main_start main_cv svref_2object opnumber perlstring
@@ -1068,6 +1068,16 @@ sub ambient_pragmas {
10681068
$self->{'ambient_hinthash'} = $hinthash;
10691069
}
10701070

1071+
sub ambient_pragmas_from_caller {
1072+
my $self = shift;
1073+
my ($hint_bits, $warning_bits, $hinthash) = (caller(0))[8, 9, 10];
1074+
$self->ambient_pragmas(
1075+
hint_bits => $hint_bits,
1076+
warning_bits => $warning_bits,
1077+
'%^H' => $hinthash,
1078+
);
1079+
}
1080+
10711081
# This method is the inner loop, so try to keep it simple
10721082
sub deparse {
10731083
my $self = shift;
@@ -7393,7 +7403,8 @@ They exist principally so that you can write code like:
73937403
); }
73947404
73957405
which specifies that the ambient pragmas are exactly those which
7396-
are in scope at the point of calling.
7406+
are in scope at the point of calling. However, see also
7407+
L</ambient_pragmas_from_caller>.
73977408
73987409
=item %^H
73997410
@@ -7402,6 +7413,16 @@ stored in the special hash %^H.
74027413
74037414
=back
74047415
7416+
=head2 ambient_pragmas_from_caller
7417+
7418+
$deparse->ambient_pragmas_from_caller()
7419+
7420+
A convenient shortcut for setting the hints and warnings ambient pragmas to
7421+
those of the immediately calling code. This uses the
7422+
L<caller|perlfunc/caller> function to determine the hints and warnings bits in
7423+
effect at the callsite to this method, and sets those as the ambient settings
7424+
for the deparser.
7425+
74057426
=head2 coderef2text
74067427
74077428
$body = $deparse->coderef2text(\&func)

lib/B/Deparse.t

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,7 @@ while (<DATA>) {
6767

6868
my $code = "$meta{context};\n" . <<'EOC' . "sub {$input\n}";
6969
# Tell B::Deparse about our ambient pragmas
70-
my ($hint_bits, $warning_bits, $hinthash);
71-
BEGIN {
72-
($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H);
73-
}
74-
$deparse->ambient_pragmas (
75-
hint_bits => $hint_bits,
76-
warning_bits => $warning_bits,
77-
'%^H' => $hinthash,
78-
);
70+
$deparse->ambient_pragmas_from_caller;
7971
EOC
8072
my $coderef = eval $code;
8173

@@ -103,17 +95,7 @@ EOC
10395
}
10496

10597
# Reset the ambient pragmas
106-
{
107-
my ($b, $w, $h);
108-
BEGIN {
109-
($b, $w, $h) = ($^H, ${^WARNING_BITS}, \%^H);
110-
}
111-
$deparse->ambient_pragmas (
112-
hint_bits => $b,
113-
warning_bits => $w,
114-
'%^H' => $h,
115-
);
116-
}
98+
$deparse->ambient_pragmas_from_caller;
11799

118100
use constant 'c', 'stuff';
119101
is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',

0 commit comments

Comments
 (0)