0
my @para_text = $mech->xpath('/html/body/form/table/tbody/tr[2]/td[3]/table/tbody/tr[2]/td/table/tbody/tr[3]/td/div/div/div', type => $mech->xpathResult('STRING_TYPE'));
#BELOW IS JUST TO MAKE SURE THE ABOVE CAPTURED THE CORRECT TEXT 
print "Look here: @para_text";

$mech->click_button( id => "lnkHdrreplyall");
$mech->eval_in_page('document.getElementsByName("txtbdy")[0].value = "@para_text"');

In the last line of my code I need to put the contents of the @para_text array as the text to output into a text box on a website however from the "document" till the end of the line it needs to be surrounded by ' ' to work. Obviously this doesnt allow interpolation as that would require " " Any ideas on what to do?

  • Try this: `'document.getElementsByName("txtbdy")[0].value = "' . "@para_text" . '"'` – Håkon Hægland Mar 31 '17 at 14:51
  • @Håkon Hægland I get MozRepl::RemoteObject: SyntaxError: unterminated string literal at Mech.pl line 59. (This is that line of code) – Jamal Mahroof Mar 31 '17 at 15:09
  • Why is that an array? If you interpolate the array into a string, all elements will be joined on `$,`, which defaults to a space `" "`. Is that what you want? What's inside `@para_text`? Maybe it has quotes? – simbabque Mar 31 '17 at 15:11
  • @simbabque: Arrays are stringified using `$"` as the separator. `$,` is the separator for `print`. – Borodin Mar 31 '17 at 16:06
  • @Borodin right, sorry. – simbabque Mar 31 '17 at 16:33
  • @simbabque Inside the array is text that need to be put into a text box on a webpage which i have attempted using the last line of code – Jamal Mahroof Apr 01 '17 at 17:19

1 Answers1

2

To define a string that itself contains double quotes as well as interpolating variable values, you may use the alternative form of the double quote qq/ ... /, where you can choose the delimiter yourself and prevent the double quote " from being special

So you can write

$mech->eval_in_page(qq/document.getElementsByName("txtbdy")[0].value = "@para_text"/)
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • MozRepl::RemoteObject: SyntaxError: unterminated string literal at Mech.pl line 60. (The line on which that code is on) – Jamal Mahroof Apr 03 '17 at 08:14
  • @JamalMahroof: You must have copied my code wrongly. It compiles fine. – Borodin Apr 03 '17 at 09:13
  • Its quite hard to incorrectly copy and paste :) perhaps your not using strawberry perl? – Jamal Mahroof Apr 03 '17 at 09:43
  • @JamalMahroof: Strawberry Perl doesn't have a different idea about strings from any other Perl. The only string terminator on that line is the slash at the end before the closing parenthesis. Are you sure that's there? And have you added a semicolon? Are there any double quotes in `@para_text`? – Borodin Apr 03 '17 at 09:47
  • $mech->eval_in_page(qq/document.getElementsByName("txtbdy")[0].value = "@para_text"/); Am I being extremely stupid and blind somewhere? There is only text inside the array. I have tried making it a string and then replacing "@para_text" in that line with "$para_text" but no luck. – Jamal Mahroof Apr 03 '17 at 10:42
  • I think maybe i need "txtbody" to be special – Jamal Mahroof Apr 03 '17 at 10:47
  • @JamalMahroof: I've just realised that that isn't a Perl error message. You must be using `WWW::Mechanize::PhantomJS`? It's JavaScript issuing that error, and it must be because you have quotes in your array as I said. Please double check. – Borodin Apr 03 '17 at 11:03
  • Ah I see what you mean, I am using WWW:Mechanize:Firefox, in the array is a date “29-Mar-2017” which has those double quotes included. The problem being that the array is filled with this text that is taken from the email on the webpage, how can i always assure that the array is checked for doubel quotes and that they get removed? – Jamal Mahroof Apr 03 '17 at 11:08
  • @JamalMahroof: Add `s/(["\\])/\\$1/g for @para_text` as a new preceding line. That will escape any double quotes or backslashes that may be in the array. I trust you don't need the contents of the array for anything else? – Borodin Apr 03 '17 at 11:17
  • The array only has one element, so i assume the loop goes through once and doesnt find a single character so it doesnt work? ive tried splitting the array up but the double quotes get replaced by these weird characters “ and †Just to note I get this output when printing the array to the cmd window ÔÇ£29-Mar-2017ÔÇØ however when putting it into a text file it comes out fine – Jamal Mahroof Apr 03 '17 at 11:47
  • When manually entering the text into the array, without any double quotes or backslashes, the code you suggested will still not input the text into the textbox, same error occurs, perhaps i should learn selenium and rewrite the program in that module? – Jamal Mahroof Apr 03 '17 at 11:59
  • @JamalMahroof: Try putting the date directly into the call: `qq/document.getElementsByName("txtbdy")[0].value = "29-Mar-2017"/`. What does that do? – Borodin Apr 03 '17 at 12:02
  • @JamalMahroof: I'm back at a PC now so I can be a bit more useful. Your array has Unicode left and right quotation marks `U+201C` and `U+201D`, which shouldn't trouble JavaScript. Please dump the whole array with `use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper \@para_text;` and tell me what you get. Please also try the date directly in the JavaScript as in my previous comment. – Borodin Apr 03 '17 at 12:17
  • @JamalMahroof: Also, show the output from `print Dumper qq/document.getElementsByName("txtbdy")[0].value = "@para_text"/;` without modifying `@para_text`. – Borodin Apr 03 '17 at 12:21
  • I get: 29-Mar-2017 The data dumper output is this (the date is only part of the array): ($VAR1 = [ "\nHi All,\n\n\x{a0}\nWe have booked a slot for\n(UK 14:00:00 BST/18:3 0 IST) \x{a0}on \x{201c}29-Mar-2017\x{201d}.\n\x{a0}\nPlease kick off the build as scheduled.\n\x{a0}\nTech Leads -> If there is any objection on build kick-off , please shout before 05:00 PM IST. Contact me for any disconnect.\x{a0}\x{a0}\x {a0}\n\n\x{a0}\nThanks,\nHimanshu\n" ]; – Jamal Mahroof Apr 03 '17 at 13:03
  • For the last line you told me to try, this comes up: print() on unopened filehandle Dumper at Mech.pl line 81, <> line 1. – Jamal Mahroof Apr 03 '17 at 13:04
  • Managed to get this from that last line you suggested: $VAR1 = "document.getElementsByName(\"txtbdy\")[0].value = \" Hi All, \x{a0} We have booked a slot for (UK 14:00:00 BST/18:30 IST) \x{a0}on \x{201c}29-Mar-2017\x{201d}. \x{a0} Please kick off the build as scheduled. \x{a0} Tech Leads -> If there is any objection on build kick-off, please shout before 0 5:00 PM IST. Contact me for any disconnect.\x{a0}\x{a0}\x{a0} \x{a0} Thanks, Himanshu \""; – Jamal Mahroof Apr 03 '17 at 13:12
  • I have also tried manually stripping the array of any punctuation whatsoever, to no luck – Jamal Mahroof Apr 03 '17 at 13:19
  • I think the issue is here XD https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unterminated_string_literal The text cant be split across lines, explains why it used to work with another string which was all on a very long single line. I spent almost a week figuring out why this wasnt working... Something so simple can break my whole program, the joys of programming XD – Jamal Mahroof Apr 03 '17 at 13:30
  • The original issue is somewhat resovled in that i can now put the text into the text box, this raises a new issue because its an email and i require the old text from the previous email to stay in the bottom of the textbox and my program to add the new reply to the top, rather what happens is the whole textbox is emptied and the new text is put in, if that makes any sense? Essentially I want to reply to an email while keeping the previous email at the bottom of the new one, which outlook automatically does, but my program deletes all text in the text box and adds the new text. – Jamal Mahroof Apr 03 '17 at 13:58
  • @JamalMahroof: Yes. You're right. I didn't realise that about JavaScript. You can escape all the special characters like that with `s/([\\"])/\\$1/g, s/\n/\\n/g for @para_text` before the call to `eval_in_page`. That includes the backslashes and double quotes that I mentioned before. Anything else really needs to be a new question, as we're way off topic now. – Borodin Apr 03 '17 at 14:07
  • Thank you for your help, been a huge aid in finding the solution :) – Jamal Mahroof Apr 03 '17 at 14:08
  • @JamalMahroof: That's okay. It took a lot longer than I imagined but I'm glad we got there. By the way, you should use the code above rather than just deleting any newlines, if that's what you've done. I'll look out for another question from you. – Borodin Apr 03 '17 at 14:17
  • I used the code you provided although i am looking into trying to get my head around it, being new to pearl im trying to understand any code people help me with. Im going to attempt to find a solution myself first as thats always the best way to learn but if need help i will be sure to post a question :) – Jamal Mahroof Apr 04 '17 at 08:02