After i changed:
system("recode ISO-8859-1..UTF-8 $csvPath");
if($? == -1) {
die(" failed\n");
}
to:
if(system("recode ISO-8859-1..UTF-8 $csvPath")) {
die(" failed!");
}
it works better but still not always.
Code snippet:
print("recoding file to utf-8...");
#`recode ISO-8859-1..UTF-8 $csvPath`;
system("recode ISO-8859-1..UTF-8 $csvPath");
if($? == -1) {
die(" failed\n");
}
print(" done!\n");
This doesn’t happen all the times. Sometimes it works sometimes not.
If it doesn’t works (script doesn’t finish in that case) the first print works, but the the second one with “done!” is never printed even if it the external command worked.
Your test
if($? == -1)doesn’t make much sense.$?will be 0 if the command succeeded, non-0 if it failed. For example, if the command did anexit(1),$?should be 256 — at least on Unix-like systems. And it’s probably cleaner to test the value returned bysystem()rather than examining$@after the fact.Try setting
$| = 1;at the top of your program; this makes your output unbuffered, and might let you see some output your script is producing before it dies.Print the exact value of the command string before executing it. If
$cvsPathis an empty string (or if it’sundefand you haven’t enabled warnings), then you’re invokingrecodewith no file argument, and it will read from standard input. That might explain the behavior you’re seeing.Though this isn’t directly relevant to your problem, if
system()is given a single argument, and if that argument contains shell metacharacters (including blanks), then the command is executed by the shell. With multiple arguments, it invokes the command directly:perldoc -f systemfor more information.And in fact, if a bad value of
$cvsPathis the cause of your problem, that could have revealed it, since it would pass an empty string as a separate argument torecode. The error messagerecodeproduces in that case is:(the empty file name is shown between the parentheses).
Note that a lot of this is guesswork based on incomplete information. Posting a small complete program that exhibits the problem would make this a lot easier.