Refactor code inside process_file() into print_preprocessor_statements()
authorJames E. Keenan <jkeenan@cpan.org>
Sat, 3 Apr 2010 19:00:02 +0000 (15:00 -0400)
committerSteffen Mueller <smueller@cpan.org>
Tue, 12 Jul 2011 18:53:54 +0000 (20:53 +0200)
dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS.pm

index ca71735..bd832f5 100644 (file)
@@ -308,34 +308,8 @@ EOF
       my $ln = shift(@{ $self->{line} });
       print $ln, "\n";
       next unless $ln =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
-      my $statement = $+;
-      if ($statement eq 'if') {
-        $XSS_work_idx = @{ $self->{XSStack} };
-        push(@{ $self->{XSStack} }, {type => 'if'});
-      }
-      else {
-        death ("Error: `$statement' with no matching `if'")
-          if $self->{XSStack}->[-1]{type} ne 'if';
-        if ($self->{XSStack}->[-1]{varname}) {
-          push(@{ $self->{InitFileCode} }, "#endif\n");
-          push(@{ $BootCode_ref },     "#endif");
-        }
-
-        my(@fns) = keys %{$self->{XSStack}->[-1]{functions}};
-        if ($statement ne 'endif') {
-          # Hide the functions defined in other #if branches, and reset.
-          @{$self->{XSStack}->[-1]{other_functions}}{@fns} = (1) x @fns;
-          @{$self->{XSStack}->[-1]}{qw(varname functions)} = ('', {});
-        }
-        else {
-          my($tmp) = pop(@{ $self->{XSStack} });
-          0 while (--$XSS_work_idx
-               && $self->{XSStack}->[$XSS_work_idx]{type} ne 'if');
-          # Keep all new defined functions
-          push(@fns, keys %{$tmp->{other_functions}});
-          @{$self->{XSStack}->[$XSS_work_idx]{functions}}{@fns} = (1) x @fns;
-        }
-      }
+      ( $XSS_work_idx, $BootCode_ref ) =
+        print_preprocessor_statements( $self, $XSS_work_idx, $BootCode_ref );
     }
 
     next PARAGRAPH unless @{ $self->{line} };
@@ -1947,4 +1921,38 @@ sub death {
   exit 1;
 }
 
+sub print_preprocessor_statements {
+  my ($self, $XSS_work_idx, $BootCode_ref) = @_;
+
+  my $statement = $+;
+  if ($statement eq 'if') {
+    $XSS_work_idx = @{ $self->{XSStack} };
+    push(@{ $self->{XSStack} }, {type => 'if'});
+  }
+  else {
+    death ("Error: `$statement' with no matching `if'")
+      if $self->{XSStack}->[-1]{type} ne 'if';
+    if ($self->{XSStack}->[-1]{varname}) {
+      push(@{ $self->{InitFileCode} }, "#endif\n");
+      push(@{ $BootCode_ref },     "#endif");
+    }
+
+    my(@fns) = keys %{$self->{XSStack}->[-1]{functions}};
+    if ($statement ne 'endif') {
+      # Hide the functions defined in other #if branches, and reset.
+      @{$self->{XSStack}->[-1]{other_functions}}{@fns} = (1) x @fns;
+      @{$self->{XSStack}->[-1]}{qw(varname functions)} = ('', {});
+    }
+    else {
+      my($tmp) = pop(@{ $self->{XSStack} });
+      0 while (--$XSS_work_idx
+           && $self->{XSStack}->[$XSS_work_idx]{type} ne 'if');
+      # Keep all new defined functions
+      push(@fns, keys %{$tmp->{other_functions}});
+      @{$self->{XSStack}->[$XSS_work_idx]{functions}}{@fns} = (1) x @fns;
+    }
+  }
+  return ($XSS_work_idx, $BootCode_ref);
+}
+
 1;