Kastagire has asked for the wisdom of the Perl Monks concerning the following question:

Greeting Perl Gurus!

I am having a bit of a quandary, as I am a complete Perl newbie, and the answer to my question is probably an easy one. In short, I have a c++ app that calls a Perl script from the command prompt. When we call the script, we need to specify several variables, like name and IP address, which I need to be variables that can be passed in. How can I accomplish this? I would like to continue to use the command window as it is, just somehow add the variables in place of "name" and "IPAddress". If you could, please provide a code example of what it should look like. This is just a small part that will be added to a much larger application, and this is how the architect wants it done. Any help you can give me would be awesome!!!

Thanks!

My code follows...

#include<iostream> #include<stdlib.h> #include<stdio.h> #include<string> #include<fstream> using namespace std; int main() { string fName; string lName; string IPAddress; bool created; cout << "First Name: " << endl; cin >> fName; cout << "Last Name: " << endl; cin >> lName; system("cmd.exe /C \"c:\\Program Files\\Forterra\\OLIVE SDK 2.0.1\ +\util\\legacy\\src\\Tool\\AddAvatar.pl\" --name=Bacon N Eggs --host=1 +27.0.0.1:9911"); // created = true; if (created == true) { cout << "Your Avatar's name is: " << fName << " " << lName << + endl; ofstream aviFile("aviList.txt", ios::app); aviFile << fName << " " << lName << endl; aviFile.close(); } else { cout <<"Your avatar was not able to be created or already exis +ts. Contact your system administrator"<< endl; } system("pause"); return 0; }

Replies are listed 'Best First'.
Re: Passing C++ variable into Perl in command line
by jethro (Monsignor) on Jul 24, 2008 at 17:09 UTC
    Do you already have the perl script and your c++ app doesn't get the call right?

    Then the spaces in 'Bacon and Eggs' might have something to do with it

    Or do you have to write the perl script now and don't know how?

    In that case Getopt::Long is definitely the right fit for the way you are calling it.

    But beware, spaces in names like Bacon N Eggs might make problems. I don't know how that can be corrected in Windows (maybe surround it with "), hopefully some other monk can answer that

Re: Passing C++ variable into Perl in command line
by toolic (Bishop) on Jul 24, 2008 at 16:57 UTC
Re: Passing C++ variable into Perl in command line
by ikegami (Patriarch) on Jul 24, 2008 at 20:31 UTC

    Replace
    --name=Bacon N Eggs
    with
    "--name=Bacon N Eggs"
    or even
    --name="Bacon N Eggs"

    system("cmd.exe /C \"c:\\Program Files\\Forterra\\OLIVE SDK 2.0.1\\uti +l\\legacy\\src\\Tool\\AddAvatar.pl\" --name=\"Bacon N Eggs\" --host=1 +27.0.0.1:9911});
Re: Passing C++ variable into Perl in command line
by moritz (Cardinal) on Jul 24, 2008 at 16:52 UTC
    Somehow I fail to see the connection to perl. Just because a perl script is being called doesn't make this problem a perl problem.

    If you want to access the command line arguments in the perl script, @ARGV and possibly Getopt::Long might help you.

Re: Passing C++ variable into Perl in command line
by spivey49 (Monk) on Jul 24, 2008 at 18:04 UTC

    You'll need to surround the command line call containing spaces with double qoutes on Windows. Here's an example using Getopt::Long:

    use Getopt::Long; GetOptions( "n=s" => \$name, "h=s" => \$host );

    Your arguments will both be strings in this example and referenced from the command line as -n and -h. From there you can reference the arguments using $name and $host