handling perl string containing '@' and '$' with system function

W

wong_powah

If I had a perl string $newpw which is inputed by a user and it may
contain special characters such as '@' and '$' (e.g. "5a@Wf7$X"). How
to pass it to the system function properly?
This does not work:
system("/usr/local/bin/acthw -pw \"$newpw\"");
__________________
 
A

A. Sinan Unur

(e-mail address removed) wrote in (e-mail address removed):
If I had a perl string $newpw which is inputed by a user and it may
contain special characters such as '@' and '$' (e.g. "5a@Wf7$X"). How
to pass it to the system function properly?
This does not work:
system("/usr/local/bin/acthw -pw \"$newpw\"");

The simplest way would be to bypass the shell:

system '/usr/local/bin/acthw', '-pw', $newpw;

perldoc -f system


Sinan
 
J

John W. Krahn

If I had a perl string $newpw which is inputed by a user and it may
contain special characters such as '@' and '$' (e.g. "5a@Wf7$X"). How
to pass it to the system function properly?
This does not work:
system("/usr/local/bin/acthw -pw \"$newpw\"");

Why does it not work? What error message, if any, does it produce?
Which version of Perl are you using? What operating system and version
are you using? Does:

/usr/local/bin/acthw -pw 5a@Wf7$X

work on the command line?



John
 
W

wong_powah

Why does it not work? What error message, if any, does it produce?
Which version of Perl are you using? What operating system and version
are you using? Does:

/usr/local/bin/acthw -pw 5a@Wf7$X

work on the command line?

John

This does not work:
system("/usr/local/bin/acthw -pw \"$newpw\"");
The error message is:
Error: The password you provided is incorrect.



# perl -v
This is perl, v5.8.8 built for i386-linux-thread-multi

Copyright 1987-2006, Larry Wall


OS is FC6 linux.
# uname -a
Linux pc1 2.6.18-1.2798.fc6 #1 SMP Mon Oct 16 14:54:20 EDT 2006 i686
i686 i386 GNU/Linux


This work ("acthw successful" message appeared), as suggested by
Sinan:
system "/usr/local/bin/acthw", "-pw", "$newpw";
 
A

A. Sinan Unur

(e-mail address removed) wrote in @a35g2000prf.googlegroups.com:
This work ("acthw successful" message appeared), as suggested by
Sinan:
system "/usr/local/bin/acthw", "-pw", "$newpw";

I am glad it worked. But here is what I originally suggested:

system '/usr/local/bin/acthw', '-pw', $newpw ;

Note the differences.

perldoc -q always

Sinan
 
B

Ben Morrow

Quoth "John W. Krahn said:
Why does it not work? What error message, if any, does it produce?
Which version of Perl are you using? What operating system and version
are you using? Does:

/usr/local/bin/acthw -pw 5a@Wf7$X

work on the command line?

Come now. Unix is a certain guess, and /bin/sh will expand $X inside
double quotes, so it's pretty clear what's going wrong. A bit more info
from the OP would have been better, but in this case it isn't necessary
to diagnose the problem.

To the OP: you can quote single arguments for the shell in a reasonably
safe and cross-platform manner using MakeMaker, though the interface is
a bit icky:

use ExtUtils::MakeMaker;

$newpw = MM->quote_literal($newpw);
system("/usr/local/bin/acthw -pw $newpw");

Note the absence of any extra quotes around $newpw: MM has handled those
for you. It is better to avoid using the shell where you can.

Ben
 
W

wong_powah

(e-mail address removed) wrote in @a35g2000prf.googlegroups.com:




I am glad it worked. But here is what I originally suggested:

system '/usr/local/bin/acthw', '-pw', $newpw ;

Note the differences.

perldoc -q always

Sinan

The difference is single quote '' and double quote "".
My concern is to handle both regular alphanumeric characters and
special characters such as '@' and '$'.
Are single quote '' and double quote "" equally good approach?
Or one approach is better than the other?
 
A

A. Sinan Unur

(e-mail address removed) wrote in

Please do not quote sigs.
The difference is single quote '' and double quote "".

That is one of the differences. Although not an iron-clad rule, I agree
with others who recommend that double-quotes be reserved for strings
where interpolation is used.

The other difference is the that my version does not interpolate the
value of $newpw. To explain that, I pointed you to the relevant FAQ
entry:

perldoc -q always
My concern is to handle both regular alphanumeric characters and
special characters such as '@' and '$'.

Shells can give special meaning to some characters. Using the list form
of system, you are avoiding the shell. I don't see why any character
should be problematic in that context.
Are single quote '' and double quote "" equally good approach?
Or one approach is better than the other?

Each one is appropriate for the task for which they are designed:
Single-quotes for non-interpolated strings, double quotes for
interpolated strings.

Sinan
 

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
474,206
Messages
2,571,069
Members
47,678
Latest member
Aniruddha Das

Latest Threads

Top