Skip to content

Commit 4f8316f

Browse files
committed
Modernize attributes.pm for v5.40
* use v5.40 * `our` on package globals * Signatures on subs * Lexical subs when not required to be visible * `module_true` means no need for ending `1;` * Use `//` operator * Remove `reftype` from attributes.xs and use builtin::reftype instead
1 parent 6c3c827 commit 4f8316f

File tree

2 files changed

+20
-45
lines changed

2 files changed

+20
-45
lines changed

ext/attributes/attributes.pm

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package attributes;
1+
package attributes 0.37;
22

3-
our $VERSION = 0.36;
3+
use v5.40;
44

5-
@EXPORT_OK = qw(get reftype);
6-
@EXPORT = ();
7-
%EXPORT_TAGS = (ALL => [@EXPORT, @EXPORT_OK]);
5+
our @EXPORT_OK = qw(get reftype);
6+
our @EXPORT = ();
7+
our %EXPORT_TAGS = (ALL => [@EXPORT, @EXPORT_OK]);
88

9-
use strict;
9+
# Older versions of this module provided a `reftype` in attributes.xs, and
10+
# there may exist code out there that relies on being able to find it.
11+
*reftype = \&builtin::reftype;
1012

1113
sub croak {
1214
require Carp;
@@ -28,27 +30,24 @@ my %msg = (
2830
const => 'Useless use of attribute "const"',
2931
);
3032

31-
sub _modify_attrs_and_deprecate {
32-
my $svtype = shift;
33+
my sub modify_attrs_and_deprecate ($svtype, @args) {
3334
# After we've removed a deprecated attribute from the XS code, we need to
3435
# remove it here, else it ends up in @badattrs. (If we do the deprecation in
3536
# XS, we can't control the warning based on *our* caller's lexical settings,
3637
# and the warned line is in this package)
3738
grep {
3839
$deprecated{$svtype} && /$deprecated{$svtype}/ ? do {
39-
require warnings;
4040
warnings::warnif('deprecated', "Attribute \"$1\" is deprecated, " .
4141
"and will disappear in Perl 5.28");
4242
0;
4343
} : $svtype eq 'CODE' && exists $msg{$_} ? do {
44-
require warnings;
4544
warnings::warnif(
4645
'misc',
4746
$msg{$_}
4847
);
4948
0;
5049
} : 1
51-
} _modify_attrs(@_);
50+
} _modify_attrs(@args);
5251
}
5352

5453
sub import {
@@ -64,7 +63,7 @@ sub import {
6463
if defined $home_stash && $home_stash ne '';
6564
my @badattrs;
6665
if ($pkgmeth) {
67-
my @pkgattrs = _modify_attrs_and_deprecate($svtype, $svref, @attrs);
66+
my @pkgattrs = modify_attrs_and_deprecate($svtype, $svref, @attrs);
6867
@badattrs = $pkgmeth->($home_stash, $svref, @pkgattrs);
6968
if (!@badattrs && @pkgattrs) {
7069
require warnings;
@@ -82,7 +81,7 @@ sub import {
8281
}
8382
}
8483
else {
85-
@badattrs = _modify_attrs_and_deprecate($svtype, $svref, @attrs);
84+
@badattrs = modify_attrs_and_deprecate($svtype, $svref, @attrs);
8685
}
8786
if (@badattrs) {
8887
croak "Invalid $svtype attribute" .
@@ -92,13 +91,11 @@ sub import {
9291
}
9392
}
9493

95-
sub get ($) {
96-
@_ == 1 && ref $_[0] or
94+
sub get :prototype($) ($svref) {
95+
ref $svref or
9796
croak 'Usage: '.__PACKAGE__.'::get $ref';
98-
my $svref = shift;
9997
my $svtype = uc reftype($svref);
100-
my $stash = _guess_stash($svref);
101-
$stash = caller unless defined $stash;
98+
my $stash = _guess_stash($svref) // scalar caller;
10299
my $pkgmeth;
103100
$pkgmeth = UNIVERSAL::can($stash, "FETCH_${svtype}_ATTRIBUTES")
104101
if defined $stash && $stash ne '';
@@ -113,7 +110,6 @@ sub require_version { goto &UNIVERSAL::VERSION }
113110
require XSLoader;
114111
XSLoader::load();
115112

116-
1;
117113
__END__
118114
#The POD goes here
119115
@@ -295,9 +291,11 @@ Otherwise, only L<built-in attributes|"Built-in Attributes"> will be returned.
295291
296292
=item reftype
297293
298-
This routine expects a single parameter--a reference to a subroutine or
299-
variable. It returns the built-in type of the referenced variable,
300-
ignoring any package into which it might have been blessed.
294+
This is an alias to L<builtin::reftype|builtin/reftype>. It is maintained
295+
here for backward compatibility for any code that expected to be able to call
296+
it from this module. Newly-written code should use the function from the
297+
L<builtin> module directly.
298+
301299
This can be useful for determining the I<type> value which forms part of
302300
the method names described in L<"Package-specific Attribute Handling"> below.
303301

ext/attributes/attributes.xs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -252,29 +252,6 @@ usage:
252252

253253
SvSETMAGIC(TARG);
254254
XSRETURN(1);
255-
256-
void
257-
reftype(...)
258-
PROTOTYPE: $
259-
PREINIT:
260-
SV *rv, *sv;
261-
dXSTARG;
262-
PPCODE:
263-
if (items != 1) {
264-
usage:
265-
croak_xs_usage(cv, "$reference");
266-
}
267-
268-
rv = ST(0);
269-
ST(0) = TARG;
270-
SvGETMAGIC(rv);
271-
if (!(SvOK(rv) && SvROK(rv)))
272-
goto usage;
273-
sv = SvRV(rv);
274-
sv_setpv(TARG, sv_reftype(sv, 0));
275-
SvSETMAGIC(TARG);
276-
277-
XSRETURN(1);
278255
/*
279256
* ex: set ts=8 sts=4 sw=4 et:
280257
*/

0 commit comments

Comments
 (0)