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

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

am 28.11.2007 02:53:21 von 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\"");
__________________

Re: handling perl string containing "@" and "$" with system function

am 28.11.2007 03:25:41 von 1usa

wong_powah@yahoo.ca wrote in news:638c7b6c-b259-4438-84c5-
3ec7fff60cef@j44g2000hsj.googlegroups.com:

> 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


--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines:

Re: handling perl string containing "@" and "$" with system function

am 28.11.2007 03:39:01 von krahnj

wong_powah@yahoo.ca wrote:
>
> 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
--
use Perl;
program
fulfillment

Re: handling perl string containing "@" and "$" with system function

am 28.11.2007 04:28:43 von wong_powah

On Nov 27, 9:39 pm, "John W. Krahn" wrote:
> wong_po...@yahoo.ca wrote:
>
> > 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
> --
> use Perl;
> program
> fulfillment

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

Re: handling perl string containing "@" and "$" with system function

am 28.11.2007 05:08:37 von 1usa

wong_powah@yahoo.ca wrote in news:f30dc5b0-5f4e-4303-999a-9cd9efebaec1
@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


--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines:

Re: handling perl string containing "@" and "$" with system function

am 28.11.2007 05:38:11 von Ben Morrow

Quoth "John W. Krahn" :
> wong_powah@yahoo.ca wrote:
> >
> > 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?

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

Re: handling perl string containing "@" and "$" with system function

am 28.11.2007 15:17:37 von wong_powah

On Nov 27, 11:08 pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> wong_po...@yahoo.ca wrote in news:f30dc5b0-5f4e-4303-999a-9cd9efebaec1
> @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
>
> --
> A. Sinan Unur <1...@llenroc.ude.invalid>
> (remove .invalid and reverse each component for email address)
> clpmisc guidelines:

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?

Re: handling perl string containing "@" and "$" with system function

am 28.11.2007 15:32:14 von 1usa

wong_powah@yahoo.ca wrote in
news:524a303d-5c39-48c9-ab09-b720df1fdda9@s12g2000prg.google groups.com:

> On Nov 27, 11:08 pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
>> wong_po...@yahoo.ca wrote in
>> news:f30dc5b0-5f4e-4303-999a-9cd9efebaec1
>> @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
>>
>> --
>> A. Sinan Unur <1...@llenroc.ude.invalid>

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
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: