Running Perl script with filename as arg
When working on my OSX box my perl script accepts my arguments with no complaints, on windows it doesnt.
Here is what im doing in cmd:
G:\perl>toxyz.pl -i "G:\perl\filename.log"
Error: invalid arguments.
G:\perl>
My perl code:
#!/usr/bin/开发者_运维百科perl -w -s
use File::Basename;
$logfile = $ARGV[0];
if(!$logfile || (!$s && !$i)){
print STDERR "Error: invalid arguments.";
exit(1);
}
This works fine on OSX. What should i do? Thanks.
In your specific example:
$ARGV[0] = '-i'
$ARGV[1] = 'G:\perl\E-Garugamblin-1-1-irc.log'
See http://perldoc.perl.org/perlvar.html#%40ARGV
Also, Windows requires command-line arguments to be surrounded by double-quotes, not single.
Finally, writing Perl without 'use strict; use warnings;' is a prescription for both frustration and in the worst case, disaster.
You realize that the right side of your || is going to return true since $s and $i aren't defined and will be seen as false.
I get this on my mac...
Name "main::s" used only once: possible typo at ./tmp.pl line 6.
Name "main::i" used only once: possible typo at ./tmp.pl line 6.
as such (!$s && !$i) will be true, and so you're entering your 'error' block
精彩评论