Platform-independent way of passing cmd-line args.

Discussion in 'Perl Misc' started by Prabh, Jun 21, 2004.

  1. Prabh

    Prabh Guest

    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
     
    Prabh, Jun 21, 2004
    #1
    1. Advertising

  2. Prabh

    Ben Morrow Guest

    Quoth (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

    --
    Joy and Woe are woven fine,
    A Clothing for the Soul divine William Blake
    Under every grief and pine 'Auguries of Innocence'
    Runs a joy with silken twine.
     
    Ben Morrow, Jun 21, 2004
    #2
    1. Advertising

  3. Prabh

    Bob Walton Guest

    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.


    ....


    > Prabh


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Jun 21, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paulo da Silva

    Parsing cmd line args problem

    Paulo da Silva, Apr 10, 2004, in forum: Python
    Replies:
    2
    Views:
    406
    Matt Gerrans
    Apr 10, 2004
  2. r
    Replies:
    2
    Views:
    264
    Gabriel Genellina
    Aug 13, 2008
  3. rusi

    trace cmd line args

    rusi, Dec 9, 2010, in forum: Python
    Replies:
    5
    Views:
    260
  4. rusi

    trace cmd line args in 2.7

    rusi, Dec 10, 2010, in forum: Python
    Replies:
    1
    Views:
    208
  5. Tim Chase
    Replies:
    2
    Views:
    205
    Miki Tebeka
    Dec 2, 2011
Loading...

Share This Page