Loading ...
Sorry, an error occurred while loading the content.

77What does this code do?

Expand Messages
  • James E Keenan
    Apr 3, 2010
    • 0 Attachment
      In lib/ExtUtils/ParseXS.pm, we find this code:

      97 for ($args{filename}) {
      98 die "Missing required parameter 'filename'" unless $_;
      99 $filepathname = $_;
      100 ($dir, $filename) = (dirname($_), basename($_));
      101 $filepathname =~ s/\\/\\\\/g;
      102 $IncludedFiles{$_}++;
      103 }

      (Taken from version 2.2203.)

      %args is a hash whose elements are first populated with default
      values and which are then augmented (or overwritten) by values set
      via command-line options. $filepathname, $dir, $filename and %
      Included Files are all global variables declared in the initial 'use
      vars' statement. None of these four variables are assigned any
      values until the code block displayed above.

      Problem: I cannot see any rationale for the 'for' loop beginning on
      line 97 and ending on line 103. There is only one element -- $args
      {filename} -- in the list over which the 'for' is iterating. Hence,
      $args{filename} is the only value which can ever be referenced by
      $_. Hence, omitting lines 97 and 103 and using $args{filename}
      instead of $_ should make no difference in our state as of the end of
      line 102.

      If $args{filename} *were* an array ref, and if line 97 read like this:

      for (@{$args{filename}}) {

      ... then the values assigned to $filepathname, $dir and $filename
      would be overwritten each time through the loop. The values those
      variables held as of line 103 would be the values derived from the
      *last* element in the list. And %IncludedFiles would have one
      element for each element in the list.

      But $args{filename} is not, AFAICT, an array ref. So, can anyone
      cite any reason why I should not refactor the block like this:

      die "Missing required parameter 'filename'" unless $args{filename};
      $filepathname = $args{filename};
      ($dir, $filename) = (dirname($args{filename}), basename($args
      {filename}));$filepathname =~ s/\\/\\\\/g;

      Thank you very much.
      Jim Keenan
    • Show all 5 messages in this topic