Extend -d:foo=bar to make -d:-foo expand to C<no foo>, consistent with -M-foo
authorNicholas Clark <nick@ccl4.org>
Thu, 25 Nov 2010 14:58:42 +0000 (14:58 +0000)
committerNicholas Clark <nick@ccl4.org>
Thu, 25 Nov 2010 14:58:42 +0000 (14:58 +0000)
perl.c
pod/perldelta.pod
pod/perlrun.pod
t/lib/Devel/switchd.pm
t/run/switchd.t

diff --git a/perl.c b/perl.c
index 438106e..d2571a8 100644 (file)
--- a/perl.c
+++ b/perl.c
@@ -3048,11 +3048,21 @@ Perl_moreswitches(pTHX_ const char *s)
        /* The following permits -d:Mod to accepts arguments following an =
           in the fashion that -MSome::Mod does. */
        if (*s == ':' || *s == '=') {
-           const char *start = ++s;
-           const char *const end = s + strlen(s);
-           SV * const sv = newSVpvs("use Devel::");
+           const char *start;
+           const char *end;
+           SV *sv;
+
+           if (*++s == '-') {
+               ++s;
+               sv = newSVpvs("no Devel::");
+           } else {
+               sv = newSVpvs("use Devel::");
+           }
+
+           start = s;
+           end = s + strlen(s);
 
-           /* We now allow -d:Module=Foo,Bar */
+           /* We now allow -d:Module=Foo,Bar and -d:-Module */
            while(isALNUM(*s) || *s==':') ++s;
            if (*s != '=')
                sv_catpvn(sv, start, end - start);
index f612dcd..f269ac3 100644 (file)
@@ -28,6 +28,30 @@ here, but most should go in the L</Performance Enhancements> section.
 
 [ List each enhancement as a =head2 entry ]
 
+=head2 C<-d:-foo> calls C<Devel::foo::unimport>
+
+The syntax C<-dI<B<:>foo>> was extended in 5.6.1 to make C<-dI<:fooB<=bar>>>
+equivalent to C<-Mfoo=bar>, which expands internally to C<use foo 'bar';>.
+F<perl> now allows prefixing the module name with C<->, with the same
+semantics as C<-M>, I<i.e.>
+
+=over 4
+
+=item C<-d:-foo>
+
+Equivalent to C<-M-foo>, expands to C<no foo;>, calls C<< foo->unimport() >>
+if the method exists.
+
+=item C<-d:-foo=bar>
+
+Equivalent to C<-M-foo=bar>, expands to C<no foo 'bar';>, calls
+C<< foo->unimport('bar') >> if the method exists.
+
+=back
+
+This is particularly useful to suppresses the default actions of a
+C<Devel::*> module's C<import> method whilst still loading it for debugging.
+
 =head1 Security
 
 XXX Any security-related notices go here.  In particular, any security
index 62c1ebb..473205a 100644 (file)
@@ -361,6 +361,7 @@ tracing module installed as Devel::foo. E.g., B<-d:DProf> executes
 the program using the Devel::DProf profiler.  As with the B<-M>
 flag, options may be passed to the Devel::foo package where they
 will be received and interpreted by the Devel::foo::import routine.
+Again, like B<-M>, use -d:-foo to call Devel::foo::unimport instead of import.
 The comma-separated list of options must follow a C<=> character.
 If B<t> is specified, it indicates to the debugger that threads
 will be used in the code being debugged.
index e5b0629..d436cd4 100644 (file)
@@ -1,6 +1,7 @@
 package Devel::switchd;
 use strict; BEGIN { } # use strict; BEGIN { ... } to incite [perl #21890]
 sub import { print "import<@_>;" }
+sub unimport { print "unimport<@_>;" }
 package DB;
 sub DB { print "DB<", join(",", caller), ">;" }
 sub sub { print "sub<$DB::sub>;"; goto &$DB::sub }
index 3cac218..3ea4681 100644 (file)
@@ -9,7 +9,7 @@ BEGIN { require "./test.pl"; }
 
 # This test depends on t/lib/Devel/switchd*.pm.
 
-plan(tests => 4);
+plan(tests => 5);
 
 my $r;
 
@@ -42,6 +42,12 @@ __SWDTEST__
                 args => ['4'],
                );
     like($r, qr/^sub<Devel::switchd::import>;import<Devel::switchd a 42>;DB<main,$::tempfile_regexp,9>;sub<Foo::foo>;DB<Foo,$::tempfile_regexp,5>;DB<Foo,$::tempfile_regexp,6>;sub<Bar::bar>;DB<Bar,$::tempfile_regexp,2>;sub<Bar::bar>;DB<Bar,$::tempfile_regexp,2>;sub<Bar::bar>;DB<Bar,$::tempfile_regexp,2>;$/);
+    $r = runperl(
+                switches => [ '-Ilib', '-f', '-d:-switchd=a,42' ],
+                progfile => $filename,
+                args => ['4'],
+               );
+    like($r, qr/^sub<Devel::switchd::unimport>;unimport<Devel::switchd a 42>;DB<main,$::tempfile_regexp,9>;sub<Foo::foo>;DB<Foo,$::tempfile_regexp,5>;DB<Foo,$::tempfile_regexp,6>;sub<Bar::bar>;DB<Bar,$::tempfile_regexp,2>;sub<Bar::bar>;DB<Bar,$::tempfile_regexp,2>;sub<Bar::bar>;DB<Bar,$::tempfile_regexp,2>;$/);
 }
 
 # [perl #71806]