More variable and code shuffling to improve maintainability
authorJames E. Keenan <jkeenan@cpan.org>
Mon, 15 Mar 2010 01:27:27 +0000 (21:27 -0400)
committerSteffen Mueller <smueller@cpan.org>
Tue, 12 Jul 2011 18:53:51 +0000 (20:53 +0200)
1. Move creation of read filehandle closer to its first point of use.

2. Move code for population of %targetable into subroutine and place that sub,
make_targetable, in EU::PXS::Utilities (for the time being).

3. Rename one instance of $name to $len_name to (start to) reduce confusion.

Note: Munged by Steffen to fix problem with order of chdir() and open()
calls.

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS.pm
dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS/Utilities.pm

index e9fee9c..8c7963b 100644 (file)
@@ -18,6 +18,7 @@ use ExtUtils::ParseXS::Utilities qw(
   C_string
   valid_proto_string
   process_typemaps
+  make_targetable
 );
 
 our (@ISA, @EXPORT_OK, $VERSION);
@@ -105,9 +106,6 @@ sub process_file {
     $IncludedFiles{$f}++;
   }
 
-  # Open the input file
-  open($FH, $args{filename}) or die "cannot open $args{filename}: $!\n";
-
   # Open the output file if given as a string.  If they provide some
   # other kind of reference, trust them that we can print to it.
   if (not ref $args{output}) {
@@ -160,29 +158,7 @@ sub process_file {
     $value =~ s/^\s+#/#/mg;
   }
 
-  my ($cast, $size);
-  our $bal;
-  $bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*]; # ()-balanced
-  $cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?]; # Optional (SV*) cast
-  $size = qr[,\s* (??{ $bal }) ]x; # Third arg (to setpvn)
-
-  my %targetable;
-  foreach my $key (keys %output_expr) {
-    # We can still bootstrap compile 're', because in code re.pm is
-    # available to miniperl, and does not attempt to load the XS code.
-    use re 'eval';
-
-    my ($t, $with_size, $arg, $sarg) =
-      ($output_expr{$key} =~
-        m[^ \s+ sv_set ( [iunp] ) v (n)?    # Type, is_setpvn
-          \s* \( \s* $cast \$arg \s* ,
-          \s* ( (??{ $bal }) )    # Set from
-          ( (??{ $size }) )?    # Possible sizeof set-from
-          \) \s* ; \s* $
-        ]x
-    );
-    $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t;
-  }
+  my %targetable = make_targetable(\%output_expr);
 
   my $END = "!End!\n\n";        # "impossible" keyword (multiple newline)
 
@@ -224,6 +200,9 @@ EOM
   print("#line 1 \"$filepathname\"\n")
     if $WantLineNumbers;
 
+  # Open the input file (using basename'd $args{filename} due to chdir above)
+  open($FH, $filename) or die "cannot open $filename: $!\n";
+
   firstmodule:
   while (<$FH>) {
     if (/^=/) {
@@ -477,7 +456,7 @@ EOF
           s/^\s+//;
           s/\s+$//;
           my ($arg, $default) = ($_ =~ m/ ( [^=]* ) ( (?: = .* )? ) /x);
-          my ($pre, $name) = ($arg =~ /(.*?) \s*
+          my ($pre, $len_name) = ($arg =~ /(.*?) \s*
                              \b ( \w+ | length\( \s*\w+\s* \) )
                              \s* $ /x);
           next unless defined($pre) && length($pre);
@@ -490,8 +469,8 @@ EOF
             $pre =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\b\s*//;
           }
           my $islength;
-          if ($name =~ /^length\( \s* (\w+) \s* \)\z/x) {
-            $name = "XSauto_length_of_$1";
+          if ($len_name =~ /^length\( \s* (\w+) \s* \)\z/x) {
+            $len_name = "XSauto_length_of_$1";
             $islength = 1;
             die "Default value on length() argument: `$_'"
               if length $default;
@@ -504,12 +483,12 @@ EOF
               push @fake_INPUT, $arg;
             }
             # warn "pushing '$arg'\n";
-            $argtype_seen{$name}++;
-            $_ = "$name$default"; # Assigns to @args
+            $argtype_seen{$len_name}++;
+            $_ = "$len_name$default"; # Assigns to @args
           }
           $only_C_inlist{$_} = 1 if $out_type eq "OUTLIST" or $islength;
-          push @outlist, $name if $out_type =~ /OUTLIST$/;
-          $in_out{$name} = $out_type if $out_type;
+          push @outlist, $len_name if $out_type =~ /OUTLIST$/;
+          $in_out{$len_name} = $out_type if $out_type;
         }
       }
       else {
@@ -803,11 +782,11 @@ EOF
           my $what = eval qq("$t->[2]");
           warn $@ if $@;
     
-          my $size = $t->[3];
-          $size = '' unless defined $size;
-          $size = eval qq("$size");
+          my $tsize = $t->[3];
+          $tsize = '' unless defined $tsize;
+          $tsize = eval qq("$tsize");
           warn $@ if $@;
-          print "\tXSprePUSH; PUSH$t->[0]($what$size);\n";
+          print "\tXSprePUSH; PUSH$t->[0]($what$tsize);\n";
           $prepush_done = 1;
         }
         else {
index 1576e82..7abaa6b 100644 (file)
@@ -14,6 +14,7 @@ our (@ISA, @EXPORT_OK);
   C_string
   valid_proto_string
   process_typemaps
+  make_targetable
 );
 
 =head1 NAME
@@ -338,4 +339,54 @@ sub process_typemaps {
   return (\%type_kind, \%proto_letter, \%input_expr, \%output_expr);
 }
 
+=head2 C<make_targetable()>
+
+=over 4
+
+=item * Purpose
+
+Populate C<%targetable>.
+
+=item * Arguments
+
+  %targetable = make_targetable(\%output_expr);
+      
+Reference to C<%output_expr>.
+
+=item * Return Value
+
+Hash.
+
+=back
+
+=cut
+
+sub make_targetable {
+  my $output_expr_ref = shift;
+  my ($cast, $size);
+  our $bal;
+  $bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*]; # ()-balanced
+  $cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?]; # Optional (SV*) cast
+  $size = qr[,\s* (??{ $bal }) ]x; # Third arg (to setpvn)
+
+  my %targetable;
+  foreach my $key (keys %{ $output_expr_ref }) {
+    # We can still bootstrap compile 're', because in code re.pm is
+    # available to miniperl, and does not attempt to load the XS code.
+    use re 'eval';
+
+    my ($t, $with_size, $arg, $sarg) =
+      ($output_expr_ref->{$key} =~
+        m[^ \s+ sv_set ( [iunp] ) v (n)?    # Type, is_setpvn
+          \s* \( \s* $cast \$arg \s* ,
+          \s* ( (??{ $bal }) )    # Set from
+          ( (??{ $size }) )?    # Possible sizeof set-from
+          \) \s* ; \s* $
+        ]x
+    );
+    $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t;
+  }
+  return %targetable;
+}
+
 1;