Reputation: 806
I am having this strange issue with Perl. I am trying to execute an external program from inside my Perl script and this external program takes string + wildcard as parameters. My Perl program looks like this
my $cmd_to_run = 'find-something-in-somedb myname* |'
open(procHandle, $cmd_to_run); # I am using open because I want to
# parse the output using pipes
For some weird reason, running this Perl script (under Windows) call to open
function ends up with error:
'sqlselect' is not recognized as an internal or external command
I guessed that its something to do with *
present in my command string and hence I removed it and now my command string looks like this
my $cmd_to_run = 'find-something-in-somedb myname|'
Now when I run my Perl script it works perfectly fine. Problem comes only when wildcard character is present.
Some points to note :
I ran the same command with wildcard char, in the same cmd prompt (where i am executing this perl script) and it works perfectly fine..
Same command works when I program it in C using _open function in Windows.
Problem seems to be only when wildcard * is present , at least that's what I am guessing
No, I haven't tried this in Unix..
Any clues???
EDIT : I found that this is something to do with ENV . The program that i am trying to run uses "sqlselect" only when "*" wild card is present in search string... Both find-something-in-somedb and sqlselect are present in same location. In which case how perl is able to find "find-in-db" and not "sqlselect"
Sorry i realize that original problem is turning out to be something else now.. Something to do with "ENV" and not with Wildcard *
Upvotes: 3
Views: 989
Reputation: 48212
It's very likely that Perl is expanding the wildcard itself, which you don't want to do. The answer provided by ephemient is very good, but in order to debug this, try calling this really simple program:
print join ' ', @ARGV;
Put that into its own file, then call it from your original program (I named mine argv.pl):
my $cmd_to_run = './argv.pl myname* |'
open(procHandle, $cmd_to_run);
That will definitively tell you on your platform how Perl is parsing things. On Unix, the * is expanded to match the files in the current working directory. Not sure about Windows though.
Upvotes: 3
Reputation: 204926
It is recommended to use the 3-argument form of open
open(procHandle, '-|', 'find-something-in-somedb', 'myname*');
as that bypasses the shell (which will perform *
expansion).
However, on Windows, applications often perform their own quote-parsing and *
expansion, so you may need
open(procHandle, '-|', 'find-something-in-somedb', '"myname*"');
or even
open(procHandle, '-|', 'find-something-in-somedb "myname*"');
as I'm not sure exactly how and when Perl hands things off to cmd
.
Upvotes: 6
Reputation: 118158
What happens if you use three-argument open
?
open my $procHandle, '-|', 'find-something-in-somedb myname*'
or die "Cannot open pipe: $!";
Upvotes: 0