0
my @para_text = (
"The build of $build CUT$cut was started as requested and 
its progress can be monitored in Anthill here", 
"",
"http://anthill:8080/tasks/project/BuildLifeTasks/viewBuildLife?
buildLifeId=$lifeid", 
"", 
'If it completes successfully (Overall Anthill status will show as green 
Complete and all sub steps as green Success) the built output will be 
available for deployment and testing by first copying the zip file from here 
\\\mizar\release\AnthillRelease\$build', 
"", "If the output exists but the anthill build was not completely 
successful DO NOT attempt to copy and deploy the output. \n", 
"We will send the usual email detailing content etc. when the build 
finishes. \n");

$para_text[0] =~ s/[\r\n]//gm;    # convert multiline to single line

@para_text = join "\n", @para_text;

s/([\\"])/\\$1/g, s/\n/\\n/g for @para_text;

$mech->eval_in_page( qq/document.getElementsByName("txtbdy")[0].value = "@para_text", "test"/ );

I have the above code.

The array holds a email template and the script is made around the Outlook webapp using WWW::Mechanize::Firefox.

The email template has a directory in it which has backslashes.

MozRepl does not allow backslashes or any special characters when putting the text into the text box at the $mech->eval_in_page line.

How can I put backslashes in the text if it's not allowed by the module?

  • *" `MozRepl` does not allow backslashes or any special characters when putting the text into the text box"* I think that's unlikely. What makes you think this is true? Also, I can see no backslashes in the message you're trying to display. – Borodin Apr 10 '17 at 14:44
  • Seems like if i put that string in single quotes it works, however i cannot interpolate the variable to complete the string (which is a directory). – Jamal Mahroof Apr 11 '17 at 08:23
  • *Unterminated string literal errors occur when any special characters are put in"* The page you link to doesn't say that. I spent a lot of time with your previous question explaining how to escape special characters in the string, and you seem to have ignored all of that. – Borodin Apr 11 '17 at 08:26
  • @Borodin My previous question? I recall someone telling me what I have regurgitated, thats why I thought that it doesnt allow special characters. – Jamal Mahroof Apr 11 '17 at 08:28
  • [*Issue with eval_in_page - Trying to interpolate an array*](http://stackoverflow.com/questions/43142971). You even have my code in this question `s/([\\"])/\\$1/g, s/\n/\\n/g for @para_text` but it's rendered mostly useless by your preceding `$para_text[0] =~ s/[\r\n]//gm` – Borodin Apr 11 '17 at 08:55
  • @Borodin I needed that preceding line because the variables caused there to be newlines in that element, even with your code. I have just tried removing the preceding code to your line that you gave me, ran the program and still got the same error, with all due respect perhaps you arent well versed in this modules syntax? – Jamal Mahroof Apr 12 '17 at 08:04
  • *"with all due respect perhaps you arent well versed in this modules syntax?"* The module's syntax is Perl syntax. I have been writing Perl professionally for sixteen years and know it inside out. The problem is that you don't understand basic debugging techniques, and insist on using complex real-life data instead of testing with something simple to find out what escaping is necessary. You come up with nonsense like *"`MozRepl` does not allow backslashes or any special characters"*, and without a proper [MCVE](https://stackoverflow.com/help/mcve) I have had to rely on you to test for me. – Borodin Apr 12 '17 at 13:58
  • If this is how you are going to respond to my efforts to help you despite your pack of ability, then I will leave you to find someone else to help you. If you want anything more from me then please create an MCVE that uses a public site so that I can do the testing myself. You should also study the concept of escape characters and do your best to understand the code I have written, as it is doubtless the core of a proper solution. – Borodin Apr 12 '17 at 14:03
  • @Borodin You say that after rejecting that my issue even exists, And also it was you who told me that special characters arent allowed in what I was doing. MozRepl integrates with firefox through javascript. Thanks for your help. (I dont understand basic debugging techniques, did you when you were three weeks into perl?) – Jamal Mahroof Apr 12 '17 at 15:26
  • *"You say that after rejecting that my issue even exists"* I am denying that `MozRepl has a thing about special characters, so in that sense no, your problem doesn't exist. See my recent answer. *"it was you who told me that special characters arent allowed in what I was doing"* I'd like to know what I wrote that you misconstrued in that way. I said that JavaScript didn't allow literal newlines inside its strings, but that's a long way from saying that "special characters" aren't allowed in "what you are doing". – Borodin Apr 12 '17 at 15:40
  • *"I dont understand basic debugging techniques, did you when you were three weeks into perl?"* Unless you've never done any programming at all before, I expect you to know how to debug. Yes, I could debug a program long before Perl had even been imagined. If you're so naive with programming then you really shouldn't go around saying of others that you've asked to help you *"perhaps you arent well versed in this modules syntax?"* – Borodin Apr 12 '17 at 15:41
  • Correction, "Did you know how to debug three weeks into programming" You also mentioned that the double quotes were causing the issue originally and that special characters arent allowed – Jamal Mahroof Apr 13 '17 at 14:36
  • Please add my tag to the start of comments addressed to me, otherwise I won't be alerted to your messages. I think you *still* don't understand what I have said. I wrote *"I didn't realise that about JavaScript"* That meant that I didn't realise that literal newlines within strings were illegal in JavaScript. It's very unusual, but that was my fault. You also can't put double quotes inside a double-quoted string; that applies to every language. – Borodin Apr 13 '17 at 19:22
  • I also wrote *"You can escape all the special characters like that with `s/([\\"])/\\$1/g, s/\n/\\n/g for @para_text`"* By *special characters like that* I meant "like newline and double quote, and also backslash itself". I don't think most people would extrapolate that to meaning ***`MozRepl` does not allow backslashes or any special characters***, and therefore that I'm ***not well versed in this modules syntax***. I think I made it clear that the problem was a JavaScript one, and only very remotely to do with Perl or `WWW::Mechanize::Firefox` or `MozRepl`. – Borodin Apr 13 '17 at 19:27
  • You can choose to be so naive that you shouldn't be expected to know how to debug your code, or so smart that you can call me out as lacking knowledge and expertise. You don't get to do both. If you can't even debug then don't dump your work on Stack Overflow: you must go away and *learn*. The problems that we get to fix here are generally from programmers with experience who have been unable to get past an issue despite their best efforts. I think you are far from putting your "best efforts" into this, and simply lack the skill and knowledge to fulfill your goals. We can't help you with that. – Borodin Apr 13 '17 at 19:33

2 Answers2

0

Take a look at this answer. I've had to create a local HTML file, and have assumed that you're using <textarea />, as a simple <input type="text" /> won't accept multiple lines

txtbdy.html

<html>
  <head>
    <title>Textbox test</title>
    <style type="text/css">
      #textbdy {
        width:  800;
        height: 300;
        resize: none;
      }
    </style>
  </head>
  <body>
    <form>
        <textarea name="txtbdy" id="textbdy" />
    </form>
  <body>
</html>

Here I've used the data from your question, with the exception of duplicating $para_text[4] to fix the interpolation of $build and to test double quotes (around "Athill")

txtbdy.pl

    use utf8;
    use strict;
    use warnings 'all';

    use WWW::Mechanize::Firefox;

    my $mech = WWW::Mechanize::Firefox->new(
        tab      => qr/Textbox test/,
        create   => 1,
        activate => 1,
    );
    $mech->autoclose_tab( 0 );

    $mech->get( 'file://E:/Perl/source/txtbdy.html' );

    my $build  = "BUILD";
    my $cut    = "CUT";
    my $lifeid = "LIFEID";

    my @para_text = (
        "The build of $build CUT$cut was started as requested and 
its progress can be monitored in Anthill here",
        "",
        "http://anthill:8080/tasks/project/BuildLifeTasks/viewBuildLife?
buildLifeId=$lifeid",
        "",
        'If it completes successfully (Overall Anthill status will show as green 
Complete and all sub steps as green Success) the built output will be 
available for deployment and testing by first copying the zip file from here 
\\\mizar\release\AnthillRelease\$build',
        "",
        qq{If it completes successfully (Overall "Anthill" status will show as green 
Complete and all sub steps as green Success) the built output will be 
available for deployment and testing by first copying the zip file from here 
\\\\mizar\\release\\AnthillRelease\\$build},
        "",
        "If the output exists but the anthill build was not completely 
successful DO NOT attempt to copy and deploy the output. \n",
        "We will send the usual email detailing content etc. when the build 
finishes. \n"
    );

    my $para_text = join "\n", @para_text;

    s/([\\"])/\\$1/g, s/\n/\\r\\n/g for $para_text;

    $mech->eval_in_page( qq/document.getElementsByName("txtbdy")[0].value = "$para_text"/ );

output

This output correctly represents the "awkward" characters newline and double quote, which would invalidate the syntax of the JavaScript string. It uses exactly the same substitution that I proposed in my comments to your earlier question Trying to interpolate an array

s/([\\"])/\\$1/g, s/\n/\\n/g for @para_text;

output from txtbdy.pl

Community
  • 1
  • 1
Borodin
  • 126,100
  • 9
  • 70
  • 144
-3

I removed the backslashes from my code and used this line to substitute them in

$para_text[4] =~ s{!}{\\}g;

Problem Solved. Substituting a character for the backslashes later on works.

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Do you mean `s/\\/!/g`? – Borodin Apr 12 '17 at 14:06
  • @Borodin No I dont, My code works, and if that code is a more efficient way of some sorts then I wouldnt expect to know it, something i need to read up on – Jamal Mahroof Apr 12 '17 at 15:23
  • So somewhere else you're replacing all exclamation marks `!` with backslashes again, and hoping the original text didn't contain any? – Borodin Apr 12 '17 at 15:34
  • This really doesn't make sense. It makes no difference at all If I add this statement to the code in your question, because there are no exclamation marks anywhere in `@para_text`, let alone `$para_text[4]`. – Borodin Apr 12 '17 at 15:44
  • Please at least turn this into an answer that others can use. By far the most important utility of Stack Overflow is that a single answer will potentially help many people. This is of no use to anyone. – Borodin Apr 12 '17 at 17:38
  • @Borodin I removed the backslashes from my code and used the above line to substitute them in – Jamal Mahroof Apr 13 '17 at 14:33
  • This question can be deleted, I dont think its very helpful anyway for anyone who stumbles across it. – Jamal Mahroof Apr 13 '17 at 14:38
  • I've edited that into your answer where it should have been in the first place, but it still makes no sense. If you've removed the backslashes and there aren't any exclamation marks then there's still nothing to substitute. There's no need to write hack code like this, and I've shown a working solution that keeps all backslashes and newlines from the original text. My only concern is that you've probably done something horrible to get the contents of `@para_text` in the first place, and you haven't deigned to show that. Any old hack ***will not do***; you should write software *properly*. – Borodin Apr 13 '17 at 19:40
  • Your own answer has *three* downvotes, and none of them are mine. Perhaps you really are doing something wrongly? – Borodin Apr 13 '17 at 19:43
  • @Borodin Im not here to play your games and compete for votes, I couldnt care less, i got my solution, Bye – Jamal Mahroof Apr 18 '17 at 08:22
  • I feel that it is you who is playing games. I am simply trying to improve the content of Stack Overflow for others who happen upon this page. This answer doesn't make sense in the context of your question, as there are no exclamation marks anywhere to be substituted. Furthermore, there is no need to remove newlines and you can preserve the layout of the message as I described in my own answer. It is simply obstinate to ignore a solution that is far better quality than your own and has been demonstrated to work with test code and data. – Borodin Apr 18 '17 at 09:39
  • So if your code simply works, why am i here raising a problem? And why did altering code fix the issue if your code was perfect oh Holy Perl Master? "Test code" can never replicate my true environment. – Jamal Mahroof Apr 18 '17 at 09:50
  • *"why am I here raising a problem?"* I don't know; you haven't explained yourself. *'"Test code" can never replicate my true environment'* no, but I was working with all the information I had, and I don't understand why you wouldn't provide more if it was incomplete. If my answer doesn't work for you then you should explain why so that I can improve it, not publish a nonsensical answer of your own. – Borodin Apr 18 '17 at 09:59
  • As I've already said, you don't have to take my word for this. Your answer has several downvotes as well, which should indicate to you that there is something wrong. It is you who are being so egotistical that you refuse to improve it even now. – Borodin Apr 18 '17 at 10:08