On 9/18/07, Ryan Moszynski <[EMAIL PROTECTED]> wrote:

> i'm using a perl script i found to change the names of batches of
> files.  The program works as is but it's giving me a weird warning.
> I'm familiar with $_, but not $_[3].  Can someone explain what $_[3]
> is, and how i can get this script to stop throwning the warning?

> 80      @_ = split /\//, $replacestr;
> 81      my $icase = '';
> 82      ($_[3] =~ /i/) and $icase = 'i';

$_[3] is the fourth element in the @_ array. Although that array is
normally used for the subroutine parameter list, it seems that this
programmer has used it to hold something else. (It would be better
style to use a different variable, or even a list of well-named
scalars. Splitting into @_ was somewhat common in Perl's ancient
history, though; maybe this script was written more than ten years
ago?) In any case, the error's source seems to be that @_ doesn't have
at least four items, I'd guess because $replacestr didn't have at
least three forward slashes and then some text.

> 83      my $cmd = sprintf("\$file =~ /%s/%s;", $_[1], $icase);
> 84
> 85      next if not eval $cmd;

But since this code is using the evil eval, I'm confident that you can
write a better (perhaps safer) program from scratch. It looks as if
the programmer didn't know that you don't have to use the evil eval in
order to be able to choose case-insensitive matches at run-time. And
maybe the programmer didn't know that Perl can interpolate into
double-quoted strings? sprintf isn't needed either.

  my $insensitive = $icase ? '(?:i)' : '';
  next if not $file =~ /$insensitive$_[1]/;

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to