LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2007, 05:24 PM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
handling perl string containing '@' and '$' with system function


If I had a perl string $newpw containing '@' 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\"");
 
Old 11-27-2007, 06:25 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
Escape like this

var1="5a\@Wf7\$X";
system("echo $var1");
 
Old 11-27-2007, 07:47 PM   #3
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by chrism01 View Post
Escape like this

var1="5a\@Wf7\$X";
system("echo $var1");
Your solution requires that I know the string in advance.
Is there a solution which allow an arbitrary string inputed by a user?
 
Old 11-27-2007, 08:33 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
This works for me:

$var1 = 'asd@fgh$hjk';
print "$var1\n";
system("echo \'$var1\' ");


output:
asd@fgh$hjk
asd@fgh$hjk

you need to enforce single quotes to prevent un-needed interpolation.
 
Old 11-27-2007, 08:51 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,609
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
"Interpolation and escaping" can become the sort of issues that cause a seasoned programmer to look at you with an expression that vaguely resembles table-tennis, because in her (or his) own mind that's rather-exactly what's going on at the time.

If you have a literal string with "magical" characters in it, such as 5a@Wf7$X, then your Perl program needs to change that string into a character sequence that the shell program, upon receiving it, will change back into the original!

(Take a moment to let the "ping-pong look" come and go...)

So what you need to do is to take a string that looks like this...

5a@Wf7$X

... and make it look like this...

5a\@Wf7\$X

... so that the shell-program you wish to run can change it back to the original ...

5a@Wf7$X

(Actually, the shell itself performs this service...)

So, what you need to do is to "escape" the string that you've been given so that the shell can then "un-escape" it back into the original string.

Something like this extemporaneously-written bit of chicken-scratching might do it:

$foo =~ s/[@$\]/\\\1/g; (ooh, ick!) might do it...

Let me try to explain...
  1. The syntax $foo =~ regexp applies a regular-expression against the variable $foo.
  2. The regular-expression is of the form s/change_from/change_to/globally...
  3. The change_from part is "either of the two characters '@' or '$' or '\'"
  4. The change_to part is 'a literal backslash character' followed by 'whatever character you found.'

If you are extremely lucky, you will happily find that this regular-expression transforms your string consistently into a string that will eventually be transformed back to the original string.

Fun stuff, huh?
 
Old 11-27-2007, 11:30 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
Yeah I started by trying something like that, but coudln't get the esc/unesc seq to perform correctly for the $ under use strict & -w, so went for single quotes eventually.
I'm sure these guys here (www.perlmonks.org) could come up with a version that does work. If you do go there, please post the answer, as I'd be glad to see it.
 
Old 11-29-2007, 06:55 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Then there's the good old 'quotemeta' function:
Code:
#! /usr/bin/perl -w

# LQpowah.pl    quotemeta demo
#
#   Usage:  LQpowah.pl file_with_metachar_strings

use strict;
    while( <> ){
        my $quotedString = quotemeta( $_  );
        print $quotedString, "\n";
    }
Code:
LQpowah.pl
5a@Wf7$X
5a\@Wf7\$X\
--- rod.
 
Old 11-30-2007, 12:54 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
For me, it wasn't so much the perl print, that was fine, it was passing it out to system and still getting a 'clean/complete' string for the receiving prog.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[perl] Convert String to Function Reference taylor_venable Programming 4 10-02-2007 01:26 PM
perl: can I make the system function timeout? apeekaboo Programming 7 07-24-2007 07:52 AM
Php String Handling joelhop Programming 1 09-11-2006 11:21 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
Perl system function syntax Crashed_Again Programming 2 03-20-2004 04:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration