Platform-independent way of passing cmd-line args.

P

Prabh

Hello all,
I need to pass a couple of args to my perl script which are strings of
text.
Arg1: 'one two'
Arg2: 'three four'

Script:
====================================================
#!/usr/local/bin/perl

use strict ;
use warnings ;

my $first_arg = $ARGV[0] ;
my $sec_arg = $ARGV[1] ;

print "First: $first_arg\nSecond: $sec_arg\n" ;
====================================================


On Unix, when I pass the args as,
<%> perl testArgs.pl 'one two' 'three four'
I get the output,
First: one two
Second: three four

When I try the same script and pass the args in the same way on
Windows, I get,
First: 'one
Second: two'

Its only when I replace the single-quote on the command-line with
double-quotes do I get the same results as Unix.
<%> perl testArgs.pl "one two" "three four"

Is there any platform independent way of passing args which result in
the same
ARGV[0] and ARGV[1] on all platforms?

Perl version: perl v5.6.0

Thanks for your time,
Prabh
 
B

Ben Morrow

Quoth (e-mail address removed) (Prabh):
On Unix, when I pass the args as,
<%> perl testArgs.pl 'one two' 'three four'
I get the output,
First: one two
Second: three four

When I try the same script and pass the args in the same way on
Windows, I get,
First: 'one
Second: two'

Its only when I replace the single-quote on the command-line with
double-quotes do I get the same results as Unix.
<%> perl testArgs.pl "one two" "three four"

Is there any platform independent way of passing args which result in
the same
ARGV[0] and ARGV[1] on all platforms?

This is not a Perl question: it is a matter of your command shell. If
you use a Unix-ish shell under win32 (cygwin bash, or there is a native
port of zsh available) then single-quotes will work fine.

In general double-quotes are pretty portable, provided you don't need
$, `, \, " or % in the arguments (unix shells will interpret the first
four, DOS/cmd.exe the last two).

Ben
 
B

Bob Walton

Prabh wrote:

....
Is there any platform independent way of passing args which result in
the same
ARGV[0] and ARGV[1] on all platforms?


No. Command line argument quoting is a function of the particular
"shell" running on a particular operating system. You only asked about
*nix and Windoze -- on Windoze, you could run Perl under Cygwin, which,
if you use its default bash shell (and, probably, any of the other
shells it provides), will let you quote arguments with ' characters like
*nix. But it will be running on Windoze.

HTH.


....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top