I have the following command
how could I add "\" (path) between v1 and bin in perl to say : c:\path\bin
as v1 = c:\path
system(dirname($v1) . $bin . " " . $trinp);
could someone recommend tutorial for perl?
Thanks
You have asked a question that has multiple answers. To just put a backslash in to form a Windows path, you need to escape the backslash thus, and this will solve your direct problem:
system($v1 . "\\" . $bin . " " . $trinp);
However, there are a few refinements. Firstly, you can use Perl's interpolation to make it easier to read:
system("$v1\\$bin $trinp");
You can also use Unix-style forward slashes, as Windows understands these, and prevents so-called "backslashitis":
system("$v1/$bin $trinp");
A better way however is to use Path::Class to generate paths on any operating system.
Further, there is a potential security problem in your code that can be caused if $trinp (or any of the other variables) contains shell metacharacters such as |
to pipe into another command. If you provide a list of arguments to the system() function, it will pass those directly to the command instead of the shell. You will probably also want to check if the command was successful, and do something useful (e.g. die()ing) if it failed:
system("$vi/$bin", $trinp) == 0
or die "Couldn't run $vi/$bin: $!";
I'll leave it as an exercise for you to convert my final example to use Path::Class.
[Edited to correct misuse use of system(), which returns the status of the command that was run which is zero for success. I confused it with more common other Perl functions which return nonzero for success.]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments