passing arguments in perl with double quotes

Discussion in 'Perl Misc' started by Samantha, Mar 4, 2004.

  1. Samantha

    Samantha Guest

    Hi,

    I would like to know how I can pass double quotes to a perl script. ie
    I have a test script which has:
    #!/usr/local/bin/perl5
    print "arg0\n";
    print @ARGV[0];
    print "\narg1\n";
    print @ARGV[1];
    print "\narg2\n";
    print @ARGV[2];
    print "\n";

    and I say:
    ../test hello "1 2"
    arg0
    hello
    arg1
    1 2
    arg2

    I would like arg1 to be "1 2" and not just 1 2
    It looks like the " are being stripped out of the argument by the
    parser. Similarly, I would like:
    ../test hello '1 2'
    arg0
    hello
    arg1
    1 2
    arg2

    to print '1 2' for arg1.

    the reson I need this is because I am passing "1 2" to another script
    which requires these arguments to have "" around them. If the " is
    missing, it only reads 1 instead of 1 2

    Thanks in advance for any help,

    Sam
    Samantha, Mar 4, 2004
    #1
    1. Advertising

  2. (Samantha) wrote in
    news::

    > Hi,
    >
    > I would like to know how I can pass double quotes to a perl script. ie
    > I have a test script which has:


    Depends on your shell. Using cmd.exe on Win2K:

    C:\Home> cat t.pl

    #! C:\opt\Perl\bin\perl.exe

    use strict;
    use warnings;

    for my $i (0 .. $#ARGV) {
    print "\$ARGV[$i] = ", $ARGV[$i], "\n";
    }

    C:\Home> perl t.pl "\"1 2 3 4 5\""
    $ARGV[0] = "1 2 3 4 5"

    C:\Home> perl t.pl "'1 2 3 4 5'"
    $ARGV[0] = '1 2 3 4 5'

    So, look it up in your shell's documentation.

    Sinan.
    A. Sinan Unur, Mar 4, 2004
    #2
    1. Advertising

  3. Samantha

    Paul Lalli Guest

    On Thu, 4 Mar 2004, Samantha wrote:

    > Hi,
    >
    > I would like to know how I can pass double quotes to a perl script. ie
    > I have a test script which has:
    > #!/usr/local/bin/perl5
    > print "arg0\n";
    > print @ARGV[0];
    > print "\narg1\n";
    > print @ARGV[1];
    > print "\narg2\n";
    > print @ARGV[2];
    > print "\n";
    >
    > and I say:
    > ./test hello "1 2"
    > arg0
    > hello
    > arg1
    > 1 2
    > arg2
    >
    > I would like arg1 to be "1 2" and not just 1 2
    > It looks like the " are being stripped out of the argument by the
    > parser. Similarly, I would like:


    This isn't perl's doing, it's the shell. Quotes are a special character
    that mean "all of the following until the next quote are one argument,
    regardless of space". If you want to include actual quote characters in
    your argument, you do:
    ../test hello \"1\ 2\"

    The second slash is now necessary because the " are no longer special, and
    so the space would normally be treated as an argument separation
    character.

    Paul Lalli
    Paul Lalli, Mar 4, 2004
    #3
  4. Paul Lalli <> wrote:
    >> I would like arg1 to be "1 2" and not just 1 2
    >> It looks like the " are being stripped out of the argument by the
    >> parser. Similarly, I would like:


    > This isn't perl's doing, it's the shell. Quotes are a special character
    > that mean "all of the following until the next quote are one argument,
    > regardless of space". If you want to include actual quote characters in
    > your argument, you do:
    > ./test hello \"1\ 2\"


    > The second slash is now necessary because the " are no longer special, and
    > so the space would normally be treated as an argument separation
    > character.


    Or just use single quotes around them (on a unix style shell)

    $ perl -le 'print foreach @ARGV' hi there
    hi
    there
    $ perl -le 'print foreach @ARGV' "hi there"
    hi there
    $ perl -le 'print foreach @ARGV' '"hi there"'
    "hi there"

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
    Darren Dunham, Mar 4, 2004
    #4
  5. Samantha

    gnari Guest

    "Paul Lalli" <> wrote in message
    news:...
    > On Thu, 4 Mar 2004, Samantha wrote:
    >
    >
    > > I would like arg1 to be "1 2" and not just 1 2
    > > It looks like the " are being stripped out of the argument by the
    > > parser. Similarly, I would like:

    >
    > This isn't perl's doing, it's the shell. Quotes are a special character
    > that mean "all of the following until the next quote are one argument,
    > regardless of space". If you want to include actual quote characters in
    > your argument, you do:
    > ./test hello \"1\ 2\"


    or
    ./test hello '"1 2"'

    gnari
    gnari, Mar 4, 2004
    #5
    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. GIMME
    Replies:
    2
    Views:
    875
    GIMME
    Feb 11, 2004
  2. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,845
    Hermit Dave
    Sep 22, 2004
  3. Sydex
    Replies:
    12
    Views:
    6,489
    Victor Bazarov
    Feb 17, 2005
  4. jOhn
    Replies:
    1
    Views:
    224
    Phlip
    Jan 29, 2008
  5. toralf
    Replies:
    5
    Views:
    171
    toralf
    Sep 14, 2009
Loading...

Share This Page