Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > turn off special characters in a string

Reply
Thread Tools

turn off special characters in a string

 
 
wong_powah@yahoo.ca
Guest
Posts: n/a
 
      09-18-2007
My string has special characters '@' and '$'.
How to escape them (turn them off)?
e.g.
$newpw is entered as "1q@W3e$R";

chop($newpw = <STDIN>);
system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");

$newpw becomes "1q@W3e" (the "$R" is chopped).

 
Reply With Quote
 
 
 
 
davidfilmer@gmail.com
Guest
Posts: n/a
 
      09-18-2007
On Sep 18, 2:22 pm, wong_po...@yahoo.ca wrote:
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> e.g.
> $newpw is entered as "1q@W3e$R";
>
> chop($newpw = <STDIN>);
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
>
> $newpw becomes "1q@W3e" (the "$R" is chopped).


perldoc -f quotemeta

FWIW, You probably want to use backticks instead of system(), and you
should check your return code.


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-19-2007
<> wrote:


> chop($newpw = <STDIN>);



You should use chomp() to remove newlines nowadays.

Using chop() to remove newlines was how it was done 10 years ago.

Where are you learning your Perl from?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      09-19-2007
wrote:
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> $newpw is entered as "1q@W3e$R";
> chop($newpw = <STDIN>);
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
> $newpw becomes "1q@W3e" (the "$R" is chopped).


This would normally not happen. I don't know
what you did else, but maybe your "quoting interpolation"
will happen only in your "test example", see:

...
my $command = 'echo'; # change this to 'changepw'

my $oldpw = '1q@W3e$Q' . "\n"; # this is how input would come from
my $newpw = '1q@W3e$R' . "\n"; # outside (note the '' quote chars!)

chomp $oldpw; # don't use chop(), always use chomp()
chomp $newpw; # if possible (as Tad already said)

# use the qq{ .. } operator to evade quote masking errors
system qq{ $command -newpw "$newpw" -oldpw "$oldpw" };
...

If your program is larger, put a

use strict;
use warnings;
...

on top of it and Perl will tell you where
unwanted interpolation might happen.

Regards

M.
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      09-19-2007
wrote:
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> e.g.
> $newpw is entered as "1q@W3e$R";
>
> chop($newpw = <STDIN>);
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
>
> $newpw becomes "1q@W3e" (the "$R" is chopped).
>


No, it's not, at least not by perl.
However, your shell will most likely do this. Try to use single quotes
instead of the double quotes:

system("changepw -newpw '$newpw' -oldpw '$oldpw'");

Note that you don't need a LF at the end of a system() string.

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      09-19-2007
On Sep 19, 12:10 am, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
> Try to use single quotes instead of the double quotes:
> system("changepw -newpw '$newpw' -oldpw '$oldpw'");


Of course, if the password contains a single-quote, this fails. And
if a user is malicious and sets their password to something like this:

x'; rm -rf /;

then your script just deleted your whole system (assuming it runs as
root, which it probably does, since it is changing passwords). Guess
what? You're fired!


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)


 
Reply With Quote
 
Mario D'Alessio
Guest
Posts: n/a
 
      09-19-2007

<> wrote in message
news: ups.com...
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> e.g.
> $newpw is entered as "1q@W3e$R";
>
> chop($newpw = <STDIN>);
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
>
> $newpw becomes "1q@W3e" (the "$R" is chopped).



To avoid escaping quotes, your system call could be (note that
you don't need the \n at the end):

system( qq( changepw -newpw "$newpw" -oldpw "$oldpw" ));

From Perl docs on "system" function: "If there is only one scalar argument,
the argument is checked for shell metacharacters, and if there are any, the
entire argument is passed to the system's command shell for parsing." So,
the command line you have is being passed to your shell for processing. Your
shell is probably interpreting the $... as variables, effectively stripping
them. To avoid this, pass multiple args to "system":

system( 'changepw', '-newpw', $newpw, '-oldpw', $oldpw );

Perl will now call changepw directly rather than using the shell.

Mario



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert between a string w/ backslashes and a string w/special characters? Peng Yu Perl Misc 3 07-13-2010 05:48 AM
Counting utf-8 characters -special characters majna Javascript 4 09-19-2007 01:53 PM
Remove only special characters and junk characters from a file rvino Perl 0 08-14-2007 07:23 AM
Re: Meta-Characters, Special Characters xah@xahlee.org Java 2 05-31-2007 09:25 AM
How to convert HTML special characters to the real characters with a Java script Stefan Mueller HTML 3 07-23-2006 10:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57