Make B::Deparse emit CORE::state, etc.
authorFather Chrysostomos <sprout@cpan.org>
Wed, 15 Jun 2011 01:09:22 +0000 (18:09 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Wed, 15 Jun 2011 04:28:42 +0000 (21:28 -0700)
Commit 9dcb83683 enabled feature.pm-enabled keywords to work with the
CORE:: prefix even without feature.pm.

B::Deparse is hereby updated to account for this, by prefixing CORE::
to those keywords if the feature in question is not loaded.

dist/B-Deparse/Deparse.pm
dist/B-Deparse/t/core.t
dist/B-Deparse/t/deparse.t

index 7496525..b37865e 100644 (file)
@@ -1095,7 +1095,9 @@ sub maybe_my {
     my $self = shift;
     my($op, $cx, $text) = @_;
     if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
-       my $my = $op->private & OPpPAD_STATE ? "state" : "my";
+       my $my = $op->private & OPpPAD_STATE
+           ? $self->keyword("state")
+           : "my";
        if (want_scalar($op)) {
            return "$my $text";
        } else {
@@ -1523,10 +1525,26 @@ sub pp_setstate { pp_nextstate(@_) }
 
 sub pp_unstack { return "" } # see also leaveloop
 
+my %feature_keywords = (
+  # keyword => 'feature',
+    state   => 'state',
+    say     => 'say',
+    given   => 'switch',
+    when    => 'switch',
+    default => 'switch',
+);
+
 sub keyword {
     my $self = shift;
     my $name = shift;
     return $name if $name =~ /^CORE::/; # just in case
+    if (exists $feature_keywords{$name}) {
+       return
+         $self->{'hinthash'}
+          && $self->{'hinthash'}{"feature_$feature_keywords{$name}"}
+           ? $name
+           : "CORE::$name";
+    }
     if (
       $name !~ /^(?:chom?p|exec|system)\z/
        && !defined eval{prototype "CORE::$name"}
@@ -1753,7 +1771,7 @@ sub givwhen {
     my $enterop = $op->first;
     my ($head, $block);
     if ($enterop->flags & OPf_SPECIAL) {
-       $head = "default";
+       $head = $self->keyword("default");
        $block = $self->deparse($enterop->first, 0);
     }
     else {
@@ -1768,8 +1786,8 @@ sub givwhen {
        "\b}\cK";
 }
 
-sub pp_leavegiven { givwhen(@_, "given"); }
-sub pp_leavewhen  { givwhen(@_, "when"); }
+sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); }
+sub pp_leavewhen  { givwhen(@_, $_[0]->keyword("when")); }
 
 sub pp_exists {
     my $self = shift;
index dcf0082..11eabc0 100644 (file)
@@ -99,3 +99,7 @@ CORE_test values => 'CORE::values %bar', 'values %hash';
 #CORE_test not => 'CORE::not $a, $b', 'not';
 CORE_test readline => 'CORE::readline $a.$b', 'readline';
 CORE_test readpipe => 'CORE::readpipe $a+$b', 'readpipe';
+
+# Tests for prefixing feature.pm-enabled keywords with CORE:: when
+# feature.pm is not enabled are in deparse.t, as they fit that for-
+# mat better.
index 7249846..6864dae 100644 (file)
@@ -738,3 +738,20 @@ $b::a[0] = 1;
 # aelemfast for a lexical
 my @a;
 $a[0] = 1;
+####
+# feature features without feature
+BEGIN {
+    delete $^H{'feature_say'};
+    delete $^H{'feature_state'};
+    delete $^H{'feature_switch'};
+}
+CORE::state $x;
+CORE::say $x;
+CORE::given ($x) {
+    CORE::when (3) {
+        continue;
+    }
+    CORE::default {
+        die;
+    }
+}