File Coverage

blib/lib/Signature/Attribute/Alias.pm
Criterion Covered Total %
statement 6 7 85.7
branch n/a
condition n/a
subroutine 3 4 75.0
pod n/a
total 9 11 81.8


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk
5              
6             package Signature::Attribute::Alias 0.01;
7              
8 3     3   498507 use v5.14;
  3         24  
9 3     3   17 use warnings;
  3         5  
  3         354  
10              
11             require XSLoader;
12             XSLoader::load( __PACKAGE__, our $VERSION );
13              
14             =head1 NAME
15              
16             C - make signature parameters that alias caller-provided values
17              
18             =head1 SYNOPSIS
19              
20             use v5.26;
21             use Sublike::Extended;
22             use Signature::Attribute::Alias;
23             use experimental 'signatures';
24              
25             extended sub trim_spaces ($s :Alias) {
26             $s =~ s/^\s+//;
27             $s =~ s/\s+$//;
28             }
29              
30             my $string = " hello, world! ";
31             trim_spaces $string;
32             say "<$string>";
33              
34             =head1 DESCRIPTION
35              
36             This module provides a third-party subroutine parameter attribute via
37             L, which declares that the parameter will alias the value
38             passed by the caller, rather than take a copy of it.
39              
40             B The ability for sublike constructions to take third-party parameter
41             attributes is still new and highly experimental, and subject to much API
42             change in future. As a result, this module should be considered equally
43             experimental. Core perl's parser does not permit parameters to take
44             attributes. This ability must be requested specially; either by using
45             L, or perhaps enabled directly by some other sublike
46             keyword using the C infrastructure.
47              
48             =head1 PARAMETER ATTRIBUTES
49              
50             =head2 :Alias
51              
52             extended sub f($x :Alias) { ... }
53              
54             Declares that the lexical variable created by the parameter will be an alias
55             to the value passed in by the caller rather than, as would normally be the
56             case, simply contain a copy it. This means that any modifications of the
57             lexical within the subroutine will be reflected in the value from the caller -
58             which, therefore - must be mutable.
59              
60             It is not automatically an error if the caller passes in an immutable value
61             (such as a constant), but any attempt to modify it will yield the usual
62             "Modification of read-only value attempted ..." warning from within the body
63             of the subroutine.
64              
65             This attribute can only be applied to positional, scalar parameters that are
66             mandatory; i.e. do not have a defaulting expression.
67              
68             =cut
69              
70             sub import
71             {
72 2     2   76 $^H{"Signature::Attribute::Alias/Alias"}++;
73             }
74              
75             sub unimport
76             {
77 0     0     delete $^H{"Signature::Attribute::Alias/Alias"};
78             }
79              
80             =head1 AUTHOR
81              
82             Paul Evans
83              
84             =cut
85              
86             0x55AA;