otijim has asked for the wisdom of the Perl Monks concerning the following question:

I posted this problem last week, but clearly I was not clear enough

I have a script that takes some user input

It then takes that string and writes (appends) some code
to the end of another perl script

Example:(pseudo-code)
#*** Script that takes user input ***
$userinput = getInput();
open THEOTHERSCRIPT ">>othersciprt";
print THEOTHERSCRIPT "print \"$userinput\"\; \n";


Now suppose the user inputs the following:
$dollars and %percents ARE "COOL!"

The script then appends the string to my OTHERSCRIPT
and the resulting script looks like such:

#*****OTHER SCRIPT******
print "$dollars and %percents ARE "COOL!"";


As you can see its not going to print what the user entered
Perl will try to do its magic on the $ and % and then the
quotes just confuse it.

So, my question is, is there an elegant way to put \ in front of
all the possible problem characters (ie $ % & " ' `, etc)
Or do I have to write a regex for each one??

I'd like to get this in one line of code ( ah hah! a challenge!)
Any takers???

Thanks!!!
Otijim
  • Comment on $dollers %percents and all other things (/) slashed

Replies are listed 'Best First'.
Re: $dollers %percents and all other things (/) slashed
by chipmunk (Parson) on Dec 11, 2000 at 22:17 UTC
    A solution: $userinput = quotemeta $userinput; quotemeta adds a backslash before each non-word character in the string.
      quotemeta did the trick!!! I never would have found that
      being a newby and all.

      Thank you much,
      otijim
Re: (jptxs) $dollers %percents and all other things (/) slashed
by jptxs (Curate) on Dec 11, 2000 at 22:37 UTC

    Ovid and chipmunk have already answered the question asked superbly, but my question to you is what exactly are you up to? I have always found that when I am writing and appending things to other scripts by opening them and directly placing text in there I am going down a bad road.

    Maybe you should make a fuller post of what you're trying to accomplish and then we can see if there's a better way to get where you're going.

    "A man's maturity -- consists in having found again the seriousness on +e had as a child, at play." --Nietzsch +e
      I'm modifying a script that the programmers use here so they can get the changes made uploaded to the servers automatically. We need them to put in some comments along with the request to upload. There are two scripts, the request script and the upload script. I cannot permenantly modify the upload script so we have the request script append a few lines of code to a temp copy of the upload script that will send out an email with the comments after the changes have been uploaded(ie the upload script is run).

      There's the purpose.
      Its pretty much just because my 'superiors' don't want the upload script modified so appending a couple lines is my work around.

        well, that's no fun at all is it? =)

        I asked because there are more graceful things one can do in place of appending to a script in many cases, but I guess you've been left with little choice through requirements placed on you - I know that feeling all too well...

        "A man's maturity -- consists in having found again the seriousness on +e had as a child, at play." --Nietzsch +e
(Ovid) Re: $dollers %percents and all other things (/) slashed
by Ovid (Cardinal) on Dec 11, 2000 at 22:19 UTC
    Why don't you just change your line to the following:
    print THEOTHERSCRIPT "print qw($userinput"; ),"\n";
    Or, you could using single quotes.

    Cheers,
    Ovid

    Update: That's twice today that I have posted to quickly. The quote mark shouldn't be in $userinput.

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      {Update: I read the parent too quickly. Ovid, your code doesn't work. It parses to print STRING; ), STRING;, does it not?}

      Well, that doesn't solve the question of $userinput containing something like I am so 1337!) && `rm -rf /` && print q(

      At minimum, with q() or single quotes, whatever character(s) close the quotes have to be escaped.

      --
      Ryan Koppenhaver, Aspiring Perl Hacker
      "I ask for so little. Just fear me, love me, do as I say and I will be your slave."