Commit | Line | Data |
80305961 |
1 | package Tie::Hash::NamedCapture; |
3195cf34 |
2 | |
192b9cd1 |
3 | our $VERSION = "0.06"; |
3195cf34 |
4 | |
192b9cd1 |
5 | # The real meat implemented in XS in universal.c in the core, but this |
6 | # method was left behind because gv.c expects a Purl-Perl method in |
7 | # this package when it loads the tie magic for %+ and %- |
44a2ac75 |
8 | |
192b9cd1 |
9 | my ($one, $all) = Tie::Hash::NamedCapture::flags(); |
44a2ac75 |
10 | |
192b9cd1 |
11 | sub TIEHASH { |
12 | my ($pkg, %arg) = @_; |
13 | my $flag = $arg{all} ? $all : $one; |
14 | bless \$flag => $pkg; |
44a2ac75 |
15 | } |
16 | |
67261566 |
17 | tie %+, __PACKAGE__; |
18 | tie %-, __PACKAGE__, all => 1; |
19 | |
44a2ac75 |
20 | 1; |
21 | |
22 | __END__ |
23 | |
24 | =head1 NAME |
25 | |
80305961 |
26 | Tie::Hash::NamedCapture - Named regexp capture buffers |
44a2ac75 |
27 | |
28 | =head1 SYNOPSIS |
29 | |
80305961 |
30 | tie my %hash, "Tie::Hash::NamedCapture"; |
3195cf34 |
31 | # %hash now behaves like %+ |
44a2ac75 |
32 | |
28d8d7f4 |
33 | tie my %hash, "Tie::Hash::NamedCapture", all => 1; |
3195cf34 |
34 | # %hash now access buffers from regexp in $qr like %- |
44a2ac75 |
35 | |
36 | =head1 DESCRIPTION |
37 | |
3195cf34 |
38 | This module is used to implement the special hashes C<%+> and C<%->, but it |
28d8d7f4 |
39 | can be used to tie other variables as you choose. |
44a2ac75 |
40 | |
3195cf34 |
41 | When the C<all> parameter is provided, then the tied hash elements will be |
42 | array refs listing the contents of each capture buffer whose name is the |
43 | same as the associated hash key. If none of these buffers were involved in |
44 | the match, the contents of that array ref will be as many C<undef> values |
45 | as there are capture buffers with that name. In other words, the tied hash |
9f8c97d1 |
46 | will behave as C<%->. |
44a2ac75 |
47 | |
3195cf34 |
48 | When the C<all> parameter is omitted or false, then the tied hash elements |
49 | will be the contents of the leftmost defined buffer with the name of the |
9f8c97d1 |
50 | associated hash key. In other words, the tied hash will behave as |
51 | C<%+>. |
44a2ac75 |
52 | |
3195cf34 |
53 | The keys of C<%->-like hashes correspond to all buffer names found in the |
54 | regular expression; the keys of C<%+>-like hashes list only the names of |
55 | buffers that have captured (and that are thus associated to defined values). |
44a2ac75 |
56 | |
44a2ac75 |
57 | =head1 SEE ALSO |
58 | |
192b9cd1 |
59 | L<perlreapi>, L<re>, L<perlmodlib/Pragmatic Modules>, L<perlvar/"%+">, |
60 | L<perlvar/"%-">. |
44a2ac75 |
61 | |
62 | =cut |