For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > perl default variable question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author perl default variable question
Ryan Moszynski

2007-09-18, 7:02 pm

hi,
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?

Thanks,

Ryan

i found the file here:

http://noisybox.net/computers/eren/

file download link:
http://noisybox.net/computers/eren/eren.pl

warning thrown:
Use of uninitialized value in pattern match (m//) at C:\pPerl\eren.pl line 82.

(i changed the file a bit so it won't be the same line number if you
checkout the whole file)

code from my file:

76 foreach my $file (@files){
77 next if -d($root . $file);
78
79 next if (($preview) and not ($file =~ /$filefilter/));
80 @_ = split /\//, $replacestr;
81 my $icase = '';
82 ($_[3] =~ /i/) and $icase = 'i';
#***************************************
***********
83 my $cmd = sprintf("\$file =~ /%s/%s;", $_[1], $icase);
84
85 next if not eval $cmd;
86
87 $preview and print "PREVIEW: ";
88 $matchct++;
89
90 my $oldfile = $file;
91 print "$oldfile --> ";
92 $file =~ eval " \$file =~ s$replacestr;";
93 print "$file\n";
94 $preview and next;
95 rename($root . $oldfile, $root . $file) or print "Failed to rename
$root$oldfile\n";
96 }
Rob Coops

2007-09-18, 7:02 pm

:-)

$_ is basically the first variable of @_ which is the array that Perl is
currently working on.

So when your script is complaining about something in $_[3] what it is
saying is that the 4th variable (Perl starts counting at 0) in the @_ array
is making the compiler unhappy.

I hope this helps a bit.
Look at line 83 of your code as you are using $_[3] in comparison, but $_[3]
might be undefined which will cause your interpeter to scream at you.



On 9/18/07, Ryan Moszynski <ryan.m.lists@gmail.com> wrote:
>
> hi,
> 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?
>
> Thanks,
>
> Ryan
>
> i found the file here:
>
> http://noisybox.net/computers/eren/
>
> file download link:
> http://noisybox.net/computers/eren/eren.pl
>
> warning thrown:
> Use of uninitialized value in pattern match (m//) at C:\pPerl\eren.pl line
> 82.
>
> (i changed the file a bit so it won't be the same line number if you
> checkout the whole file)
>
> code from my file:
>
> 76 foreach my $file (@files){
> 77 next if -d($root . $file);
> 78
> 79 next if (($preview) and not ($file =~ /$filefilter/));
> 80 @_ = split /\//, $replacestr;
> 81 my $icase = '';
> 82 ($_[3] =~ /i/) and $icase = 'i';
> #***************************************
***********
> 83 my $cmd = sprintf("\$file =~ /%s/%s;", $_[1], $icase);
> 84
> 85 next if not eval $cmd;
> 86
> 87 $preview and print "PREVIEW: ";
> 88 $matchct++;
> 89
> 90 my $oldfile = $file;
> 91 print "$oldfile --> ";
> 92 $file =~ eval " \$file =~ s$replacestr;";
> 93 print "$file\n";
> 94 $preview and next;
> 95 rename($root . $oldfile, $root . $file) or print "Failed to rename
> $root$oldfile\n";
> 96 }
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


Tom Phoenix

2007-09-18, 7:02 pm

On 9/18/07, Ryan Moszynski <ryan.m.lists@gmail.com> 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
Ryan Moszynski

2007-09-18, 7:02 pm

On 9/18/07, Ryan Moszynski <ryan.m.lists@gmail.com> wrote:
> thanks,
>
> so i can just test to see if $_[3]
>
> exists
>
> and it kills the warning.
>
> On 9/18/07, Andrew Curry <andrew.curry@pa-sport.com> wrote:
>

John W. Krahn

2007-09-18, 7:02 pm

Ryan Moszynski wrote:
> hi,


Hello,

> i'm using a perl script i found


You should be careful with stuff you find lying around.

> 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?


$_[3] is the fourth element of the array @_.


> i found the file here:
>
> http://noisybox.net/computers/eren/
>
> file download link:
> http://noisybox.net/computers/eren/eren.pl


Did you read the second line of that file?

# this program sucks and is a total hack and needs TONS of work...


> warning thrown:
> Use of uninitialized value in pattern match (m//) at C:\pPerl\eren.pl line 82.


This means that the fourth element of the array @_ contains the value undef.


> (i changed the file a bit so it won't be the same line number if you
> checkout the whole file)
>
> code from my file:
>
> 76 foreach my $file (@files){
> 77 next if -d($root . $file);
> 78
> 79 next if (($preview) and not ($file =~ /$filefilter/));
> 80 @_ = split /\//, $replacestr;
> 81 my $icase = '';
> 82 ($_[3] =~ /i/) and $icase = 'i';
> #***************************************
***********
> 83 my $cmd = sprintf("\$file =~ /%s/%s;", $_[1], $icase);
> 84
> 85 next if not eval $cmd;


As the comment says, this sucks. You can accomplish the same thing without
using eval.


> 86
> 87 $preview and print "PREVIEW: ";
> 88 $matchct++;
> 89
> 90 my $oldfile = $file;
> 91 print "$oldfile --> ";
> 92 $file =~ eval " \$file =~ s$replacestr;";
> 93 print "$file\n";
> 94 $preview and next;
> 95 rename($root . $oldfile, $root . $file) or print "Failed to rename
> $root$oldfile\n";
> 96 }




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com