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

Hi, I want to call a PERL program from MS-DOS batch file. Following is just an example, the string may inturn have double quote or single quote. I can't call with arg1, arg2 etc, as the input will be dyanamic. My argument may be something like this "204.120.69.195" "-""-" 'xyz' "GET" Perl should receive the whole string as a single argument. I know it is not a perl problem, From MS-DOS how can I sent it so that perl program treats it as a single argument. Can you please tell me how I could send the whole thing as a single argument to the PERL program. If give the solution, I would be really grateful to you. Thanks & regards, Dinesh Sharma sharmadi@india.hp.com

20040210 Edit by BazB: Changed title from 'Double quote problem, Pls. help, Here is the complete question'

Replies are listed 'Best First'.
Re: Double quote problem, Pls. help
by Abigail-II (Bishop) on Feb 10, 2004 at 14:18 UTC
    That's not a Perl (it's Perl, the language, or perl, the binary. It's not spelled PERL) problem. That's a problem to be solved by the environment you call the Perl program from. If you call it from a C program, look up the documentation of the exec* family of functions. If you call it from a shell, lookup the quoting constructs of your shell (usually, including the argument in single or double quotes does the trick). If you call it from another Perl program, lookup the documentation of system or exec.

    If you call it from something else, lookup the documentation of something else.

    Abigail

Re: Double quote problem.
by hmerrill (Friar) on Feb 10, 2004 at 14:24 UTC
    I'm not sure I understand your question, but you could concatenate them all into one string, like this:
    my $arg1 = "204.120.69.195"; my $arg2 = "-"; my $arg3 = "-"; my $arg4 = "GET"; my $single_arg = $arg1 . $arg2 . $arg3 . $arg4; ### $single_arg now equals "204.120.69.195--GET"; my $rc = system("your_perl_script.pl", $single_arg);
    and then execute the script passing the new contatenated single argument to the script.

    HTH.

Re: Double quote problem
by Limbic~Region (Chancellor) on Feb 10, 2004 at 19:29 UTC
    sharmadi,
    MS-DOS batch file arguments are stored in the following variables:  %1 %2 %3 %4 %5 %6 %7 %8 %9 Unfortunately, you only get 9 so if there are more arguments than that you will have to shift them down to single digits. %0 is used for the batch file name itself.

    So assuming you will never have more than 9 command line arguments you would do it like this:

    @echo off c:\perl\bin\perl.exe myscript.pl "%1%2%3%4%5%6%7%8%9"
    The arguments will be concatenated together and available inside the myscript.pl in the $ARGV[0] variable. If you need an example of how to handle more than 9 arguments let me know via /msg since that really isn't a perl question.

    Cheers - L~R

      In Windows NT, you can use %* to represent all parameters. I have not tried this with more than 9, though. I would say that this is close enough to on topic since it is to do with scripting, and many languishing in Windows Land end up wrapping their perl scripts in polyglot batch language.
Re: Double quote problem.
by Roger (Parson) on Feb 10, 2004 at 14:18 UTC
    Well, that's not really a perl issue, it's to do with the shell you are using. I assume that you are on a *nix platform, running ksh - then you can put the argument inside single quotes, if I remember correctly, certainly worthwhile to give it a try anyway -
    perlscript.pl '"204.120.69.195" "-""-" "GET"'

Re: Double quote problem, Pls. help, Here is the complete question
by jonadab (Parson) on Feb 10, 2004 at 19:45 UTC

    Whether this is even possible, much less how to do it, depends on which version of DOS you have and specifically which command interpreter. (What does it say if you type VER at the prompt?)

    Assuming what you have is either really MS-DOS as such or the "DOS" that's included in Windows 9x/Me (not e.g. the "DOS" included in Windows XP, which is quite different), your command interpreter is almost surely command.com, and you should see the documentation (such as it is) for that. Depending on your installation, you can *probably* get help for this using the help command (i.e., type HELP at the prompt), though I don't recall how detailed the help for <Command> or whether it goes into quoting. If not, you might be able to find an old DOS manual someplace. I have one at home from ITT DOS that I could consult when I get home. If you don't find the answer in the next several hours, /msg me with a reminder to look at that.

    I believe that in DOS versions 5 and later and all versions of Windows 9x/Me what you want to do is (just barely) possible, but the syntax is a bit weird and I no longer recall the exact details. (I haven't done serious BATch programming since I started using QBASIC circa 1995, and I quit using QBASIC very much after I got comfortable with elisp circa 2001; these days I do most stuff in Perl.)

    You might be better served to install an alternative command processor, such as 4DOS (which is shareware) or bash (which is free from delorie.com last I checked -- look for DJGPP). If what you're using is not really DOS (e.g., if you're really using the "MS-DOS Prompt" in Windows), then there are additional options available to you, such as Cygwin; I think there's also a mingw32 port of bash someplace, which might be easier for you to integrate into your existing environment.

    Another option that is available to you regardless is to replace the BAT file with a Perl script that subsumes its functionality. This is probably what I would do in your shoes. If you post the BAT file here, I can probably help you translate it to Perl.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: Double quote problem
by graff (Chancellor) on Feb 11, 2004 at 01:43 UTC
    Why not get a Windows port of the bash shell (from Cygwin or Gnu), and use normal shell scripts instead of DOS batch files? A bash shell on Windows even knows how to treat a perl script as an executable program.