summaryrefslogtreecommitdiff
path: root/ext/Tie-Hash-NamedCapture
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2010-10-14 13:29:22 +0100
committerNicholas Clark <nick@ccl4.org>2010-10-14 14:34:28 +0100
commit8cf6f931c54936a38eedd3d17d5fa1d5af1b9009 (patch)
treed1454e028f6b92f0458daaf6522aac4117323a00 /ext/Tie-Hash-NamedCapture
parent17b8ae88c658cb5fe05998ab824e6c02cfba7dcd (diff)
downloadperl-8cf6f931c54936a38eedd3d17d5fa1d5af1b9009.tar.gz
Convert lib/Tie/Hash/NamedCapture.pm to an XS module in ext/
Initially move only Tie::Hash::NamedCapture::flags from universal.c to it.
Diffstat (limited to 'ext/Tie-Hash-NamedCapture')
-rw-r--r--ext/Tie-Hash-NamedCapture/NamedCapture.pm62
-rw-r--r--ext/Tie-Hash-NamedCapture/NamedCapture.xs14
2 files changed, 76 insertions, 0 deletions
diff --git a/ext/Tie-Hash-NamedCapture/NamedCapture.pm b/ext/Tie-Hash-NamedCapture/NamedCapture.pm
new file mode 100644
index 0000000000..065d68deff
--- /dev/null
+++ b/ext/Tie-Hash-NamedCapture/NamedCapture.pm
@@ -0,0 +1,62 @@
+use strict;
+package Tie::Hash::NamedCapture;
+
+our $VERSION = "0.07";
+
+require XSLoader;
+XSLoader::load(__PACKAGE__);
+
+my ($one, $all) = Tie::Hash::NamedCapture::flags();
+
+sub TIEHASH {
+ my ($pkg, %arg) = @_;
+ my $flag = $arg{all} ? $all : $one;
+ bless \$flag => $pkg;
+}
+
+tie %+, __PACKAGE__;
+tie %-, __PACKAGE__, all => 1;
+
+1;
+
+__END__
+
+=head1 NAME
+
+Tie::Hash::NamedCapture - Named regexp capture buffers
+
+=head1 SYNOPSIS
+
+ tie my %hash, "Tie::Hash::NamedCapture";
+ # %hash now behaves like %+
+
+ tie my %hash, "Tie::Hash::NamedCapture", all => 1;
+ # %hash now access buffers from regexp in $qr like %-
+
+=head1 DESCRIPTION
+
+This module is used to implement the special hashes C<%+> and C<%->, but it
+can be used to tie other variables as you choose.
+
+When the C<all> parameter is provided, then the tied hash elements will be
+array refs listing the contents of each capture buffer whose name is the
+same as the associated hash key. If none of these buffers were involved in
+the match, the contents of that array ref will be as many C<undef> values
+as there are capture buffers with that name. In other words, the tied hash
+will behave as C<%->.
+
+When the C<all> parameter is omitted or false, then the tied hash elements
+will be the contents of the leftmost defined buffer with the name of the
+associated hash key. In other words, the tied hash will behave as
+C<%+>.
+
+The keys of C<%->-like hashes correspond to all buffer names found in the
+regular expression; the keys of C<%+>-like hashes list only the names of
+buffers that have captured (and that are thus associated to defined values).
+
+=head1 SEE ALSO
+
+L<perlreapi>, L<re>, L<perlmodlib/Pragmatic Modules>, L<perlvar/"%+">,
+L<perlvar/"%-">.
+
+=cut
diff --git a/ext/Tie-Hash-NamedCapture/NamedCapture.xs b/ext/Tie-Hash-NamedCapture/NamedCapture.xs
new file mode 100644
index 0000000000..b51dedae78
--- /dev/null
+++ b/ext/Tie-Hash-NamedCapture/NamedCapture.xs
@@ -0,0 +1,14 @@
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+
+MODULE = Tie::Hash::NamedCapture PACKAGE = Tie::Hash::NamedCapture
+PROTOTYPES: DISABLE
+
+void
+flags(...)
+ PPCODE:
+ EXTEND(SP, 2);
+ mPUSHu(RXapif_ONE);
+ mPUSHu(RXapif_ALL);
+