The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
Changes 04
META.yml 36
Makefile.PL 01
lib/String/RewritePrefix.pm 522
t/basic.t 624
5 files changed (This is a version diff) 1457
@@ -1,5 +1,9 @@
 Revision history for String-RewritePrefix
 
+0.005     2009-11-30
+          export "rewrite" with Sub::Exporter
+          prefix new_rewriter with a _; it was always private
+
 0.004     2009-02-20
           allow coderef prefix generators (thanks FLORA)
 
@@ -1,21 +1,24 @@
 --- #YAML:1.0
 name:               String-RewritePrefix
-version:            0.004
+version:            0.005
 abstract:           ~
 author:  []
 license:            perl
 distribution_type:  module
 configure_requires:
     ExtUtils::MakeMaker:  0
+build_requires:
+    ExtUtils::MakeMaker:  0
 requires:
-    Test::More:  0.47
+    Sub::Exporter:  0
+    Test::More:     0.47
 resources:
     Repository:  http://github.com/rjbs/string-rewriteprefix
 no_index:
     directory:
         - t
         - inc
-generated_by:       ExtUtils::MakeMaker version 6.48
+generated_by:       ExtUtils::MakeMaker version 6.55_02
 meta-spec:
     url:      http://module-build.sourceforge.net/META-spec-v1.4.html
     version:  1.4
@@ -7,6 +7,7 @@ WriteMakefile(
   (eval { ExtUtils::MakeMaker->VERSION(6.21) } ? (LICENSE => 'perl') : ()),
   PREREQ_PM     => {
     'Test::More' => '0.47',
+    'Sub::Exporter' => 0,
   },
   (eval { ExtUtils::MakeMaker->VERSION(6.46) }
     ? (META_MERGE => {
@@ -3,13 +3,17 @@ use warnings;
 package String::RewritePrefix;
 use Carp ();
 
+use Sub::Exporter -setup => {
+  exports => [ rewrite => \'_new_rewriter' ],
+};
+
 =head1 NAME
 
 String::RewritePrefix - rewrite strings based on a set of known prefixes
 
 =head1 VERSION
 
-version 0.004
+version 0.005
 
 =head1 SYNOPSIS
 
@@ -22,9 +26,21 @@ version 0.004
   # now you have:
   qw(MyApp::Plugin MyApp::Mixin MyApp::Addon Corporate::Thinger)
 
+You can also import a rewrite routine:
+
+  use String::RewritePrefix rewrite => {
+    -as => 'rewrite_dt_prefix',
+    prefixes => { '' => 'MyApp::', '+' => '' },
+  };
+
+  my @to_load = rewrite_dt_prefix( qw(Plugin Mixin Addon +Corporate::Thinger));
+
+  # now you have:
+  qw(MyApp::Plugin MyApp::Mixin MyApp::Addon Corporate::Thinger)
+
 =cut
 
-our $VERSION = '0.004';
+our $VERSION = '0.005';
 
 =head1 METHODS
 
@@ -43,11 +59,12 @@ as its only argument.  The return value will be used as the prefix.
 
 sub rewrite {
   my ($self, $arg, @rest) = @_;
-  return $self->new_rewriter($arg)->(@rest);
+  return $self->_new_rewriter(rewrite => { prefixes => $arg })->(@rest);
 }
 
-sub new_rewriter {
-  my ($self, $rewrites) = @_;
+sub _new_rewriter {
+  my ($self, $name, $arg) = @_;
+  my $rewrites = $arg->{prefixes} || {};
 
   my @rewrites;
   for my $prefix (sort { length $b <=> length $a } keys %$rewrites) {
@@ -1,14 +1,17 @@
 use strict;
 use warnings;
 
-use Test::More tests => 2;
+use Test::More tests => 3;
 
 use String::RewritePrefix;
 
-my $rewriter = String::RewritePrefix->new_rewriter({
-  '-' => 'Tet::',
-  '@' => 'KaTet::',
-  '+' => sub { $_[0] . '::Foo::' },
+# testing this method directly seems excessive -- rjbs, 2009-11-30
+my $rewriter = String::RewritePrefix->_new_rewriter(undef, {
+  prefixes => {
+    '-' => 'Tet::',
+    '@' => 'KaTet::',
+    '+' => sub { $_[0] . '::Foo::' },
+  },
 });
 
 my @results = $rewriter->(qw(
@@ -25,7 +28,6 @@ is_deeply(
   "rewrote prefices",
 );
 
-
 my @to_load = String::RewritePrefix->rewrite(
   { '' => 'MyApp::', '+' => '' },
   qw(Plugin Mixin Addon +Corporate::Thinger),
@@ -37,3 +39,19 @@ is_deeply(
   "from synopsis, code okay",
 );
 
+{
+  String::RewritePrefix->import(
+    rewrite => { -as => 'pfx_rw', prefixes => {
+      '-' => 'minus ',
+      '+' => 'plus ',
+      ''  => 'plus ',
+    } }
+  );
+  
+  is_deeply(
+    [ pfx_rw(qw(+10 10 -10 0)) ],
+    [ 'plus 10', 'plus 10', 'minus 10', 'plus 0' ],
+    'rewrote with import',
+  );
+}
+