passing arguments in perl with double quotes

S

Samantha

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
 
A

A. Sinan Unur

(e-mail address removed) (Samantha) wrote in
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.
 
P

Paul Lalli

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
 
D

Darren Dunham

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"
 
G

gnari

Paul Lalli said:
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
 

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

Forum statistics

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

Latest Threads

Top